1. 程式人生 > >Android整合百度地圖(三)POI檢索

Android整合百度地圖(三)POI檢索

      有時候我們需要在地圖上搜索周邊資訊,我們可以用百度地圖提供的POI檢索。

      POI(Point of Interest),中文可以翻譯為“興趣點”。在地理資訊系統中,一個POI可以是一棟房子、一個商鋪、一個郵筒、一個公交站等。百度地圖SDK提供三種類型的POI檢索:城市內檢索、周邊檢索和區域檢索(即矩形範圍檢索)。

POI檢索

1.建立POI檢索例項

 PoiSearch mPoiSearch;
 mPoiSearch = PoiSearch.newInstance();

2.建立POI檢索監聽者

OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){  

    public void onGetPoiResult(PoiResult result){  
    //獲取POI檢索結果  
    }  

    public void onGetPoiDetailResult(PoiDetailResult result){  
    //獲取Place詳情頁檢索結果  
    }  
};

3.設定POI檢索監聽者

mPoiSearch.setOnGetPoiSearchResultListener(poiListener);

4.發起檢索請求

城市檢索

根據關鍵字檢索適用於在某個城市搜尋某個名稱相關的POI,例如:杭州的沙縣小吃。

mPoiSearch.searchInCity((new PoiCitySearchOption())  
    .city(“杭州”)  
    .keyword(“沙縣小吃”)  
    .pageNum(10));

區域檢索

POI區域檢索以“使用者指定的左下角和右上角座標的長方形區域”為檢索範圍的poi檢索。

LatLng southwest = new LatLng( 39.92235, 116.380338 );

LatLng northeast = new LatLng( 39.947246, 116.414977);

LatLngBounds searchbound = new LatLngBounds.Builder()

.include(southwest).include(northeast)

.build();

mPoiSearch.searchInBound(new PoiBoundSearchOption().bound(searchbound)
       .keyword("餐廳"));

周邊檢索

周邊搜尋是一個圓形範圍,適用於以某個位置為中心點,自定義檢索半徑值,搜尋某個位置附近的POI。

  mPoiSearch.searchNearby(new PoiNearbySearchOption()
	    .keyword("餐廳")
		.sortType(PoiSortType.distance_from_near_to_far)
		.location(center)
		.radius(radius)
		.pageNum(10));

5.釋放POI檢索例項

 mPoiSearch.destroy();

6.顯示檢索結果覆蓋物

構造自定義 PoiOverlay 類;

private class MyPoiOverlay extends PoiOverlay { 
 
    public MyPoiOverlay(BaiduMap baiduMap) {  
        super(baiduMap);  
    }  

    @Override  

    public boolean onPoiClick(int index) {  
        super.onPoiClick(index);  
        return true;  
    }  
}

在POI檢索回撥介面中新增自定義的PoiOverlay;

public void onGetPoiResult(PoiResult result) {  

    if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {  
        return;  
    }  

    if (result.error == SearchResult.ERRORNO.NO_ERROR) {  

        mBaiduMap.clear();  

        //建立PoiOverlay  

        PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);  

        //設定overlay可以處理標註點選事件  

        mBaiduMap.setOnMarkerClickListener(overlay);  

        //設定PoiOverlay資料  

        overlay.setData(result);  

        //新增PoiOverlay到地圖中  

        overlay.addToMap();  
        overlay.zoomToSpan();  
        return;  
    }  
}

注意如果找不到PoiOerlay類,請看這篇https://blog.csdn.net/flyrunlx/article/details/51428399

 完整程式碼如下:
public class MainActivity extends AppCompatActivity {


    //百度地圖控制元件
    private MapView mapView;
    //百度地圖物件
    private BaiduMap baiduMap;
    private PoiSearch mPoiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //該方法注意要在setContentView之前實現
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        /**
         * 地圖初始化
         */
        //獲取百度地圖控制元件
        mapView = (MapView) findViewById(R.id.baiduMapView);
        //獲取百度地圖物件
        baiduMap = mapView.getMap();

        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
        mPoiSearch.searchInCity((new PoiCitySearchOption())
                .city("杭州")
                .keyword("沙縣小吃")
                .pageNum(10));
    }


    private class MyPoiOverlay extends PoiOverlay {

        public MyPoiOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override

        public boolean onPoiClick(int index) {
            super.onPoiClick(index);
            return true;
        }
    }

    OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {

        public void onGetPoiResult(PoiResult result) {

            //獲取POI檢索結果
            if (result == null
                    || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 沒有找到檢索結果
                Toast.makeText(MainActivity.this, "未找到結果",
                        Toast.LENGTH_LONG).show();
                return;
            } else if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                if (result != null) {

                    baiduMap.clear();

                    //建立PoiOverlay

                    PoiOverlay overlay = new MyPoiOverlay(baiduMap);

                    //設定overlay可以處理標註點選事件

                    baiduMap.setOnMarkerClickListener(overlay);

                    //設定PoiOverlay資料

                    overlay.setData(result);

                    //新增PoiOverlay到地圖中

                    overlay.addToMap();
                    overlay.zoomToSpan();
                    return;
                }

                mPoiSearch.destroy();

            }
        }
        public void onGetPoiDetailResult(PoiDetailResult result) {
            //獲取Place詳情頁檢索結果
            result.getAddress();
        }

        @Override
        public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {


        }
    };

    @Override
    protected void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        mapView = null;
        super.onDestroy();
    }
}

第一次進入的時候,返回的錯誤碼是:SearchResult.ERRORNO.PERMISSION_UNFINISHED。

注意:在SDK各功能元件使用之前都需要呼叫

SDKInitializer.initialize(getApplicationContext());,因此我們建議該方法放在Application的初始化方法中

主要是網路驗證走子執行緒,在顯示介面時,不一定完成授權。在專案中一般放在Application的初始化方法中,練習的話,關閉demo再進一次就好了。

最後效果如圖: