標籤:android 百度地圖 poi搜尋
轉載請標明出處:http://blog.csdn.net/tanzuai/article/details/43835431
目前百度地圖SDK所整合的檢索服務包括:POI檢索、公交資訊查詢、線路規劃、地理編碼、線上建議查詢、短串分享。
本篇部落格將先介紹POI檢索和線上建議查詢(在地圖地位功能基礎上實現的,還不知道定位的童靴,請參考Android 百度地圖 SDK v3.3.0 (二)--- 地圖定位和圖層展示)
百度地圖SDK提供三種類型的POI檢索:周邊檢索、地區檢索和城市內檢索。下面將以城市內檢索為例,向大家介紹如何使用檢索服務。
在介紹檢索服務之前,想上一張要實現的:
好了!現在我們上代碼,來實現上面的功能(代碼中都做了相應的註解)
1.建立POI和線上建議查詢執行個體
/** * 初始化搜尋模組,註冊搜尋事件監聽 */private void initPOIListener() {//POI檢索執行個體mPoiSearch = PoiSearch.newInstance();//建立POI檢索監聽者mPoiSearch.setOnGetPoiSearchResultListener(this);//聯想詞檢索執行個體mSuggestionSearch = SuggestionSearch.newInstance();//聯想詞檢索監聽者mSuggestionSearch.setOnGetSuggestionResultListener(this);}
2.建立POI檢索監聽者,並做相關的事件處理
@Overridepublic void onGetSuggestionResult(SuggestionResult res) {if (res == null || res.getAllSuggestions() == null) {return;}sugAdapter.clear();for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {if (info.key != null)sugAdapter.add(info.key);}sugAdapter.notifyDataSetChanged();}@Overridepublic void onGetPoiDetailResult(PoiDetailResult result) {// 未找到了結果if (result.error != SearchResult.ERRORNO.NO_ERROR) {Toast.makeText(MainActivity.this, "抱歉,未找到結果", Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this,result.getName() + ": " + result.getAddress(),Toast.LENGTH_SHORT).show();}}@Overridepublic void onGetPoiResult(PoiResult result) {// 未找到結果if (result == null|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {Toast.makeText(MainActivity.this, "未找到結果", Toast.LENGTH_LONG).show();return;}// 結果沒有異常,找到了結果if (result.error == SearchResult.ERRORNO.NO_ERROR) {mBaiduMap.clear();PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);mBaiduMap.setOnMarkerClickListener(overlay);overlay.setData(result);overlay.addToMap();overlay.zoomToSpan();return;}// 當輸入關鍵字在本市沒有找到,但在其他城市找到時,返回包含該關鍵字資訊的城市列表if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {String strInfo = "在";for (CityInfo cityInfo : result.getSuggestCityList()) {strInfo += cityInfo.city;strInfo += ",";}strInfo += "找到結果";Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_LONG).show();}}/** * 提供了在地圖上標識搜尋結果的方法 * * @author TanZuAi * */private class MyPoiOverlay extends PoiOverlay {public MyPoiOverlay(BaiduMap baiduMap) {super(baiduMap);}@Overridepublic boolean onPoiClick(int index) {super.onPoiClick(index);// 擷取poi覆蓋物的詳細資料PoiInfo poi = getPoiResult().getAllPoi().get(index);// uid是POI檢索中擷取的POI ID資訊mPoiSearch.searchPoiDetail((new PoiDetailSearchOption()).poiUid(poi.uid));return true;}}
3.設定按鈕的相關事件。
/** * 影響搜尋按鈕點擊事件 * * @param v */public void searchButtonProcess(View v) {EditText editCity = (EditText) findViewById(R.id.city);// 輸入的城市EditText editSearchKey = (EditText) findViewById(R.id.searchkey);// 輸入的關鍵詞// 發起檢索請求mPoiSearch.searchInCity((new PoiCitySearchOption()).city(editCity.getText().toString())// 根據城市.keyword(editSearchKey.getText().toString())// 根據關鍵字.pageNum(load_Index));// 查詢的頁數}/** * 每添加一頁進行查詢 * * @param v */public void goToNextPage(View v) {load_Index++;searchButtonProcess(null);} 4.為了節省電量和資源,得設定搜尋的相關生命週期
@Overrideprotected void onDestroy() {mPoiSearch.destroy();mSuggestionSearch.destroy();// 在activity執行onDestroy時執行mMapView.onDestroy(),實現地圖生命週期管理mMapView.onDestroy();super.onDestroy();}@Overrideprotected void onResume() {super.onResume();// 在activity執行onResume時執行mMapView. onResume (),實現地圖生命週期管理mMapView.onResume();}@Overrideprotected void onPause() {super.onPause();// 在activity執行onPause時執行mMapView. onPause (),實現地圖生命週期管理mMapView.onPause();}
好了!代碼實現已經介紹完畢!相信大家都看的懂!有什麼不懂的或者建議,可以在下面留言!
下面是本篇部落格的源碼:
源碼
Android 百度地圖 SDK v3_3_0 (五) ---POI搜尋和線上建議查詢功能