轉載 Android SDK for 百度地圖【Demo興趣搜索】

百度地圖SDK爲開發者提供了便捷的檢索服務。今天我將爲你們介紹Poi檢索相關的內容。
        首先,咱們要構建一個最基本的地圖應用,具體介紹請參考: Android sdk
        在這個工程的基礎之上咱們作必定的修改。
        第一步,修改佈局文件,添加關鍵字輸入框和用於執行搜索操做的按鈕。代碼以下:
?
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);
        }
    }
});
    

    第六步,完成!結果以下圖所示: 佈局

243

 

  注意:本示例只給出了最基本的核心代碼,須要查看所有代碼的開發者請下載原工程文件。
        此外,這個簡單的例子旨在說明Poi的最基本使用方法及執行邏輯,百度地圖SDK爲開發者提供了豐富的檢索服務,您能夠實現城市檢索、周邊檢索、範圍檢索等等。
轉載自: http://www.android-study.com/jiemiansheji/545.html
相關文章
相關標籤/搜索