我想大家可能都有过这样的经历:兜里揣着一张银行卡,在街上到处找自动取款机(atm)。在这个场景中,atm就是的兴趣点,我们想做的事情就是找到离自己较近的一些atm然后取款,此时我们并不关心附近有哪些超市、酒吧,因为这些地方没办法取钱!
说了这么多,一方面是加深大家对poi这个词的认识,另一方面也是为了让大家明白我们接下来要做的事情。理论性的东西就不再多讲了,直接来看例子。
先给大家展示下效果图:
详细界面:
该示例主要介绍关键词查询、suggestion查询和查看餐饮类place详情页功能,尤其搜索某个地方的餐厅、理发店等等比较有实际意义,百度demo代码如下:
activity:
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
package com.home;
import android.app.activity;
import android.os.bundle;
import android.text.editable;
import android.text.textwatcher;
import android.view.view;
import android.widget.arrayadapter;
import android.widget.autocompletetextview;
import android.widget.edittext;
import android.widget.toast;
import com.baidu.mapapi.bmapmanager;
import com.baidu.mapapi.map.mapview;
import com.baidu.mapapi.search.mkaddrinfo;
import com.baidu.mapapi.search.mkbuslineresult;
import com.baidu.mapapi.search.mkdrivingrouteresult;
import com.baidu.mapapi.search.mkpoiinfo;
import com.baidu.mapapi.search.mkpoiresult;
import com.baidu.mapapi.search.mksearch;
import com.baidu.mapapi.search.mksearchlistener;
import com.baidu.mapapi.search.mkshareurlresult;
import com.baidu.mapapi.search.mksuggestioninfo;
import com.baidu.mapapi.search.mksuggestionresult;
import com.baidu.mapapi.search.mktransitrouteresult;
import com.baidu.mapapi.search.mkwalkingrouteresult;
/**
* 演示poi搜索功能
*/
public class poisearchactivity extends activity {
private mapview mmapview = null ;
private mksearch msearch = null ; // 搜索模块,也可去掉地图模块独立使用
/**
* 搜索关键字输入窗口
*/
private autocompletetextview keyworldsview = null ;
private arrayadapter<string> sugadapter = null ;
private int load_index;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
demoapplication app = (demoapplication) this .getapplication();
if (app.mbmapmanager == null ) {
app.mbmapmanager = new bmapmanager( this );
app.mbmapmanager.init(demoapplication.strkey,
new demoapplication.mygenerallistener());
}
setcontentview(r.layout.activity_poisearch);
mmapview = (mapview) findviewbyid(r.id.bmapview);
mmapview.getcontroller().enableclick( true );
mmapview.getcontroller().setzoom( 12 );
// 初始化搜索模块,注册搜索事件监听
msearch = new mksearch();
msearch.init(app.mbmapmanager, new mksearchlistener() {
// 在此处理详情页结果
@override
public void ongetpoidetailsearchresult( int type, int error) {
if (error != 0 ) {
toast.maketext(poisearchactivity. this , "抱歉,未找到结果" ,
toast.length_short).show();
} else {
toast.maketext(poisearchactivity. this , "成功,查看详情页面" ,
toast.length_short).show();
}
}
/**
* 在此处理poi搜索结果
*/
public void ongetpoiresult(mkpoiresult res, int type, int error) {
// 错误号可参考mkevent中的定义
if (error != 0 || res == null ) {
toast.maketext(poisearchactivity. this , "抱歉,未找到结果" ,
toast.length_long).show();
return ;
}
// 将地图移动到第一个poi中心点
if (res.getcurrentnumpois() > 0 ) {
// 将poi结果显示到地图上
mypoioverlay poioverlay = new mypoioverlay(
poisearchactivity. this , mmapview, msearch);
poioverlay.setdata(res.getallpoi());
mmapview.getoverlays().clear();
mmapview.getoverlays().add(poioverlay);
mmapview.refresh();
// 当epoitype为2(公交线路)或4(地铁线路)时, poi坐标为空
for (mkpoiinfo info : res.getallpoi()) {
if (info.pt != null ) {
mmapview.getcontroller().animateto(info.pt);
break ;
}
}
} else if (res.getcitylistnum() > 0 ) {
// 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
string strinfo = "在" ;
for ( int i = 0 ; i < res.getcitylistnum(); i++) {
strinfo += res.getcitylistinfo(i).city;
strinfo += "," ;
}
strinfo += "找到结果" ;
toast.maketext(poisearchactivity. this , strinfo,
toast.length_long).show();
}
}
public void ongetdrivingrouteresult(mkdrivingrouteresult res,
int error) {
}
public void ongettransitrouteresult(mktransitrouteresult res,
int error) {
}
public void ongetwalkingrouteresult(mkwalkingrouteresult res,
int error) {
}
public void ongetaddrresult(mkaddrinfo res, int error) {
}
public void ongetbusdetailresult(mkbuslineresult result, int ierror) {
}
/**
* 更新建议列表
*/
@override
public void ongetsuggestionresult(mksuggestionresult res, int arg1) {
if (res == null || res.getallsuggestions() == null ) {
return ;
}
sugadapter.clear();
for (mksuggestioninfo info : res.getallsuggestions()) {
if (info.key != null )
sugadapter.add(info.key);
}
sugadapter.notifydatasetchanged();
}
@override
public void ongetshareurlresult(mkshareurlresult result, int type,
int error) {
}
});
keyworldsview = (autocompletetextview) findviewbyid(r.id.searchkey);
sugadapter = new arrayadapter<string>( this ,
android.r.layout.simple_dropdown_item_1line);
keyworldsview.setadapter(sugadapter);
/**
* 当输入关键字变化时,动态更新建议列表
*/
keyworldsview.addtextchangedlistener( new textwatcher() {
@override
public void aftertextchanged(editable arg0) {
}
@override
public void beforetextchanged(charsequence arg0, int arg1,
int arg2, int arg3) {
}
@override
public void ontextchanged(charsequence cs, int arg1, int arg2,
int arg3) {
if (cs.length() <= 0 ) {
return ;
}
string city = ((edittext) findviewbyid(r.id.city)).gettext()
.tostring();
/**
* 使用建议搜索服务获取建议列表,结果在onsuggestionresult()中更新
*/
msearch.suggestionsearch(cs.tostring(), city);
}
});
}
@override
protected void onpause() {
mmapview.onpause();
super .onpause();
}
@override
protected void onresume() {
mmapview.onresume();
super .onresume();
}
@override
protected void ondestroy() {
mmapview.destroy();
super .ondestroy();
}
@override
protected void onsaveinstancestate(bundle outstate) {
super .onsaveinstancestate(outstate);
mmapview.onsaveinstancestate(outstate);
}
@override
protected void onrestoreinstancestate(bundle savedinstancestate) {
super .onrestoreinstancestate(savedinstancestate);
mmapview.onrestoreinstancestate(savedinstancestate);
}
/**
* 影响搜索按钮点击事件
*
* @param v
*/
public void searchbuttonprocess(view v) {
edittext editcity = (edittext) findviewbyid(r.id.city);
edittext editsearchkey = (edittext) findviewbyid(r.id.searchkey);
msearch.poisearchincity(editcity.gettext().tostring(), editsearchkey
.gettext().tostring());
}
public void gotonextpage(view v) {
// 搜索下一组poi
int flag = msearch.gotopoipage(++load_index);
if (flag != 0 ) {
toast.maketext(poisearchactivity. this , "先搜索开始,然后再搜索下一组数据" ,
toast.length_short).show();
}
}
}
|
布局xml(activity_poisearch):
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
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:orientation= "vertical" >
<linearlayout
android:layout_width= "fill_parent"
android:layout_height= "50dip"
android:orientation= "horizontal" >
<textview
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "在" >
</textview>
<edittext
android:id= "@+id/city"
android:layout_width= "wrap_content"
android:layout_height= "match_parent"
android:text= "北京" />
<textview
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "市内找" >
</textview>
<autocompletetextview
android:id= "@+id/searchkey"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_weight= "0.88"
android:text= "餐厅" />
</linearlayout>
<linearlayout
android:layout_width= "fill_parent"
android:layout_height= "50dip"
android:orientation= "horizontal" >
<button
android:id= "@+id/search"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:layout_weight= "12"
android:background= "@drawable/button_style"
android:onclick= "searchbuttonprocess"
android:padding= "10dip"
android:text= "开始" />
<button
android:id= "@+id/map_next_data"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:layout_weight= "12"
android:background= "@drawable/button_style"
android:onclick= "gotonextpage"
android:padding= "10dip"
android:text= "下一组数据" />
</linearlayout>
<com.baidu.mapapi.map.mapview
android:id= "@+id/bmapview"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:clickable= "true" />
</linearlayout>
|
mypoioverlay类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.home;
import android.app.activity;
import com.baidu.mapapi.map.mapview;
import com.baidu.mapapi.map.poioverlay;
import com.baidu.mapapi.search.mkpoiinfo;
import com.baidu.mapapi.search.mksearch;
public class mypoioverlay extends poioverlay {
mksearch msearch;
public mypoioverlay(activity activity, mapview mapview, mksearch search) {
super (activity, mapview);
msearch = search;
}
@override
protected boolean ontap( int i) {
super .ontap(i);
mkpoiinfo info = getpoi(i);
if (info.hascaterdetails) {
msearch.poidetailsearch(info.uid);
}
return true ;
}
}
|
在配置文件中要比之前多配置一个activity,不然没法查看详细界面,这是百度sdk jar中提供的类:
1
2
3
4
5
|
<activity
android:name= "com.baidu.mapapi.search.placecateractivity"
android:configchanges= "orientation|keyboardhidden"
android:theme= "@android:style/theme.notitlebar" >
</activity>
|
application类同之前。