使用腾讯地图SDK的自动补全功能

上一篇讲了高德地图的自动补全

这一篇来讲一讲腾讯地图的使用

大体上都差不多,只是集成步骤有点区别而已…

说明

本文只使用了腾讯地图的 自动提示 功能

本文只使用了腾讯地图的 自动提示 功能

本文只使用了腾讯地图的 自动提示 功能

其他2D,3D,地图街景等功能请自行查阅官方文档

另外,如果想用高德地图的朋友

请看我的上一篇博客:使用高德地图SDK的自动补全功能

一、集成腾讯地图SDK

1.集成检索SDK有两种方法 - 基于AS,使用Eclipse的童鞋请参考官方文档

使用 maven

矢量地图 v4.0.3.2 开始支持 maven 配置地图sdk,配置方法:

工程顶级 gradle.properties 文件中加入:

1
2
3
maven{
url "https://oss.sonatype.org/content/groups/public"
}

module build.gradle 文件中添加依赖库名称:

1
2
3
4
dependencies {
//这里始终使用最新的稳定版本,用户也可以指定 4.0.3.2 之后的地图 sdk 版本号
compile 'com.tencent.map:tencent-map-vector-sdk:latest.release'
}

使用JAR包

下载地址:http://lbs.qq.com/android_v1/log_search.html

解压并且把TencentSearch_v1.x.x.jar 加入到libs目录下,右键add as library

参考文档:http://lbs.qq.com/android_v1/guide-project-setup.html

这样就完成了第一步。

2.申请key

申请开发者账号:http://lbs.qq.com/console/user_info.html

需要填写手机和邮箱

进入控制台以后,就可以看到自己的KEY的,在授权应用设置自己的包名

3.在app -> AndroidManifest.xml 加入:

1
2
3
<meta-data
android:name="TencentMapSDK"
android:value="输入申请的开发者权限"/>

4.混淆

1
2
3
#腾讯地图检索sdk
-keep class com.tencent.lbssearch.**{*;}
-keep class com.google.gson.examples.android.model.** { *; }

完成了工程设置以后,就是撸代码的时间了~

二、开始撸代码

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即可


本文结束啦感谢您的阅读