安卓搜索附近商店,结果根据距离远近排序并返回列表,进行显示。

安卓将本地的位置与要搜索的内容发送给后端,后端模糊搜索返回列表

布局


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SearchActivity">

    <EditText
        android:id="@+id/editTextsearch"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_marginTop="72dp"
        android:layout_marginEnd="100dp"
        android:layout_marginRight="100dp"
        android:ems="10"


        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.343"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.964"
        app:layout_constraintStart_toEndOf="@+id/editTextsearch"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_search" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
       />
</androidx.constraintlayout.widget.ConstraintLayout>

发送信息并显示结果在listview

本地位置信息获取方法详见以前的博文
链接: link.

  public void Search() throws InterruptedException
    {

   Thread t1=  new Thread(new Runnable()
        {
            @Override
            public void run()
            {

                try
                {
               //发送搜索信息与位置坐标
                    word= (String) text_search.getText().toString();

                    RequestBody formBody = new FormBody.Builder()


                        .add("word", String.valueOf(word))
                        .add("longitude", String.valueOf(mCurrentLongitude))
                        .add("lantitude", String.valueOf(mCurrentLantitude))
                        .build();
                    OkHttpClient client = new OkHttpClient();

                    Request request = new Request.Builder()
                            .url("http://10.0.2.2:8080/Search")
                            .post(formBody)
                            .build();

                    Response response = null;
                    response = client.newCall(request).execute();
                    s=response.body().string();
                    //获得返回列表
                    array = new JSONArray(s);
                         yCurrentLantitude=new String[array.length()];
                        yCurrentLongitude=new String[array.length()];
                    for (int i = 0; i < array.length(); i++)
                    {
                        JSONObject object = array.getJSONObject(i);

                        String advertise = object.getString("advertise");
                           yCurrentLongitude[i]=object.getString("longitude");
                           yCurrentLantitude[i]=object.getString("lantitude");
                           list.add(advertise);


                    }




                } catch (IOException | JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });
        //等待子线程结束
   t1.start();
   t1.join();
        if (list!=null)
        {
            //讲结果展示到listview上
            adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,list);
        }
        listView.setAdapter(adapter);
    }
}

后端代码

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class Searchcontroller
{

    private static final long serialVersionUID = 1L;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public Searchcontroller()
    {
        super();
    }
    @ResponseBody
    @PostMapping("Search")
    public List GetLocation (@RequestParam("word") String word,@RequestParam("lantitude") String lantitude, @RequestParam("longitude") String longitude)
    {




        List list=null;
//模糊搜索,并且对查询到的结果根据经度差的平方与纬度差平方和的大小来进行排序。
        String sql = "select lantitude,longitude ,advertise from table_user_password where advertise like "+"'"+"%"+word+"%"+"'"+"order by ( lantitude -"+lantitude
                +") *(lantitude -"+lantitude+") +"+"( longitude-"+longitude
                +") *(longitude -"+longitude+") ";





        list=jdbcTemplate.queryForList(sql);
        System.out.println(list.get(0));
        return list;
    }




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值