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);
}
}
});
|
第六步,完成!結果以下圖所示: 佈局