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
|
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/android"
xmlns:tools="
http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- 放入百度地图的mapview -->
<com.baidu.mapapi.map.MapView android:id="@+id/bmapsView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"/>
<!-- 用户输入关键字的文本框 -->
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/button1"
android:hint="请输入搜索关键字"
android:ems="50" >
<requestFocus />
</EditText>
<!-- 执行Poi搜索的按钮 -->
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="《搜索Poi》" />
</RelativeLayout>
|
第二步,在主类中定义EditText和Button对象,并初始化。代码以下: html
1
2
3
4
5
6
|
// 初始化关键词输入框和按钮控件
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
|
第三步,定义并初始化搜索监听对象(这里咱们只对poi搜索作了监听,若是开发者在使用其余检索时,只需修改对应的监听方法便可)。代码以下: android
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
|
MKSearchListener mkSearchListener = new MKSearchListener() {
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
// 首先判断是否搜索到结果
if(arg2 != 0 || arg0 == null)
{
Toast.makeText(MainActivity.this, "没有找到结果!", Toast.LENGTH_SHORT).show();
return;
}
// 将结果绘制到地图上
if(arg0.getCurrentNumPois() > 0)
{
PoiOverlay poiOverlay = new PoiOverlay(MainActivity.this, mapView);
poiOverlay.setData(arg0.getAllPoi());
mapView.getOverlays().clear();
mapView.getOverlays().add(poiOverlay);
mapView.refresh();
//当arg1为2(公交线路)或4(地铁线路)时, poi坐标为空
for( MKPoiInfo info : arg0.getAllPoi() )
{
if ( info.pt != null ){
mapView.getController().animateTo(info.pt);
break;
}
}
}
}
@Override
public void onGetPoiDetailSearchResult(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
// TODO Auto-generated method stub
}
};
|
第四步,定义Poi检索对象并初始化。代码以下: api
1
2
3
4
|
// 初始化Poi搜索对象
mkSearch = new MKSearch();
mkSearch.init(bMapManager, mkSearchListener);
|
第五步,设置button的点击事件,实现Poi搜索。代码以下: ide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String key = editText.getText().toString();
// 若是关键字为空则不进入搜索
if(key.equals(""))
{
Toast.makeText(MainActivity.this, "请输入搜索关键词!", Toast.LENGTH_SHORT).show();
}
else
{
// 这里Poi搜索以城市内Poi检索为例,开发者可根据本身的实际需求,灵活使用
mkSearch.poiSearchInCity("北京", key);
}
}
});
|
第六步,完成!结果以下图所示: 布局