使用高德地图SDK的自动补全功能

2018转眼即至 又是新的一年

祝大家新年大吉,狗年旺旺!

本文:使用高德地图SDK的自动补全地址功能

前言

最近一直加班赶项目进度,有很多想写的博客都没时间写,趁过年抽空把遇到的问题和一些心得记下来,这一篇是介绍如何使用高德地图SDK的自动补全地址功能

一、集成高德地图SDK

下载地址:http://lbs.amap.com/api/android-sdk/download/

解压,把jar包复制到libs文件夹,右键add as library

因为之前项目已经集成高德地图了,所以申请秘钥,加入权限,这些步骤就省略了。

二、创建搜索代码

1,创建activity_poi_keyword_search.xml布局文件,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:id="@+id/et_keyword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:hint="请输入地址关键字搜索"
android:paddingLeft="8dp"
android:textColor="@color/deep_black"
android:textSize="14sp"
/>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/line_color"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

</LinearLayout>

2,创建recyclerview的item布局item_poi_keyword_search.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/ll_item_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
>

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/line_color"/>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="8dp">

<TextView
android:id="@+id/tv_detailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@color/deep_black"
android:textSize="16sp"/>

<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@color/black"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>

3,创建实体类PoiAddressBean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.Serializable;

public class PoiAddressBean implements Serializable {

private String longitude;//经度
private String latitude;//纬度
private String text;//信息内容
public String detailAddress;//详细地址
public String province;//省
public String city;//城市
public String district;//区域

public PoiAddressBean(String lon, String lat, String detailAddress, String text, String province, String city, String district){
this.longitude = lon;
this.latitude = lat;
this.text = text;
this.detailAddress = detailAddress;
this.province = province;
this.city = city;
this.district = district;


}

public String getLongitude() {
return longitude;
}

public String getLatitude() {
return latitude;
}

public String getText() {
return text;
}

public String getDetailAddress() {
return detailAddress;
}

public String getProvince() {
return province;
}

public String getCity() {
return city;
}

public String getDistrict() {
return district;
}

}

4,创建适配器PoiKeywordSearchAdapter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alpha58.poidemo.activity.PoiKeywordSearchActivity;
import com.alpha58.poidemo.R;
import com.alpha58.poidemo.bean.PoiAddressBean;
import java.util.List;

public class PoiKeywordSearchAdapter extends RecyclerView.Adapter<PoiKeywordSearchAdapter.MyViewHolder> {

List<PoiAddressBean> poiAddressBean;
Context mContext;
public PoiKeywordSearchAdapter(Context context, List<PoiAddressBean> poiAddressBean) {
this.poiAddressBean = poiAddressBean;
this.mContext = context;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(mContext).inflate(R.layout.item_poi_keyword_search, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

final PoiAddressBean poiAddressBean = this.poiAddressBean.get(position);
holder.tv_detailAddress.setText(poiAddressBean.getDetailAddress());
holder.tv_content.setText(poiAddressBean.getText());
holder.ll_item_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((PoiKeywordSearchActivity)mContext).setDetailAddress(poiAddressBean.getDetailAddress());
}
});
}

@Override
public int getItemCount() {
if (poiAddressBean != null) {
return poiAddressBean.size();
} else {
return 0;
}
}

class MyViewHolder extends RecyclerView.ViewHolder {

TextView tv_content;
TextView tv_detailAddress;
LinearLayout ll_item_layout;

public MyViewHolder(View itemView) {
super(itemView);
tv_detailAddress = (TextView) itemView.findViewById(R.id.tv_detailAddress);
tv_content = (TextView) itemView.findViewById(R.id.tv_content);
ll_item_layout = (LinearLayout) itemView.findViewById(R.id.ll_item_layout);
}
}
}

5,创建搜索界面类PoiKeywordSearchActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.alpha58.poidemo.R;
import com.alpha58.poidemo.adapter.PoiKeywordSearchAdapter;
import com.alpha58.poidemo.bean.PoiAddressBean;
import com.alpha58.poidemo.util.ToastUtil;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;
import java.util.List;

public class PoiKeywordSearchActivity extends AppCompatActivity implements PoiSearch.OnPoiSearchListener {


private RecyclerView mRecyclerView;
private EditText mEt_keyword;
private String keyWord = "";// 要输入的poi搜索关键字
private PoiResult poiResult; // poi返回的结果
private int currentPage = 0;// 当前页面,从0开始计数
private PoiSearch.Query query;// Poi查询条件类
private PoiSearch poiSearch;// POI搜索

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poi_keyword_search);

initView();
initListener();
initData();
}


private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mEt_keyword = (EditText) findViewById(R.id.et_keyword);
}

private void initListener() {
mEt_keyword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
keyWord = String.valueOf(charSequence);
if ("".equals(keyWord)) {
ToastUtil.show(PoiKeywordSearchActivity.this,"请输入搜索关键字");
return;
} else {
doSearchQuery();
}
}

@Override
public void afterTextChanged(Editable editable) {

}
});
}

/**
* 开始进行poi搜索
*/
protected void doSearchQuery() {
currentPage = 0;
//不输入城市名称有些地方搜索不到
query = new PoiSearch.Query(keyWord, "", "深圳");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
//这里没有做分页加载了,默认给50条数据
query.setPageSize(50);// 设置每页最多返回多少条poiitem
query.setPageNum(currentPage);// 设置查第一页

poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
}

private void initData() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

}


/**
* POI信息查询回调方法
*/
@Override
public void onPoiSearched(PoiResult result, int rCode) {
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getQuery() != null) { // 搜索poi的结果
if (result.getQuery().equals(query)) { // 是否是同一条
poiResult = result;
ArrayList<PoiAddressBean> data = new ArrayList<PoiAddressBean>();//自己创建的数据集合
// 取得搜索到的poiitems有多少页
List<PoiItem> poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
List<SuggestionCity> suggestionCities = poiResult
.getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
for(PoiItem item : poiItems){
//获取经纬度对象
LatLonPoint llp = item.getLatLonPoint();
double lon = llp.getLongitude();
double lat = llp.getLatitude();

String title = item.getTitle();
String text = item.getSnippet();
String provinceName = item.getProvinceName();
String cityName = item.getCityName();
String adName = item.getAdName();
data.add(new PoiAddressBean(String.valueOf(lon), String.valueOf(lat), title, text,provinceName,
cityName,adName));
}
PoiKeywordSearchAdapter adapter = new PoiKeywordSearchAdapter(PoiKeywordSearchActivity.this,data);
mRecyclerView.setAdapter(adapter);
}
} else {
ToastUtil.show(PoiKeywordSearchActivity.this,
getString(R.string.no_result));
}
} else {
ToastUtil.showerror(this, rCode);
}

}

/**
* POI信息查询回调方法
*/
@Override
public void onPoiItemSearched(PoiItem item, int rCode) {
// TODO Auto-generated method stub

}


/**
* 设置详情地址
* @param detailAddress
*/
public void setDetailAddress(String detailAddress) {
mEt_keyword.setText(detailAddress);
}
}

6.在AndroidManifest中注册PoiKeywordSearchActivity

7.在需要用到的地方startActivity跳转到PoiKeywordSearchActivity即可

更新

除了高德地图,我还试了一下腾讯地图SDK的自动提示功能。

对比了一下,感觉还是腾讯的地址提示更接近常用的选项,目前已经从高德转到腾讯了。

文章地址:使用腾讯地图SDK的自动补全功能


本文结束啦感谢您的阅读
  • 本文标题: 使用高德地图SDK的自动补全功能
  • 本文作者: BlueLzy
  • 创建时间: 2018年02月17日 - 16时02分
  • 引用链接: https://www.bluelzy.com/articles/amap_search.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!