實作android百度地圖搜尋功能
本篇主要實作基于目前位置實作周圍2000米的搜尋,将在上次的代碼基礎上,實作過程如下圖所示:
- 先在mapView上面添加一個editText搜尋框,實作搜尋監聽事件
- 在mapView上面添加PoiSearch圖層,主要用于搜尋結果描繪
- 對查找出來的地點圖示mark添加監聽事件,彈出其具體資訊視窗展示
1、 MainActivit重寫onCreate
MainActivity實作接口OnEditorActionListener, OnGetPoiSearchResultListener,在上次
代碼的基礎上,在onCreate方法裡添加以下代碼
// searchs為搜尋框、mPoiSearch為PoiSearch圖層
searchs.setOnEditorActionListener(this);
mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(this);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.showZoomControls(false);
baiduMap = mMapView.getMap();
baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo());
// 開啟定位圖層
baiduMap.setMyLocationEnabled(true);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setAddrType("detail");
option.setCoorType("bd09ll");
// 定位到目前位置
locClient = new LocationClient(this);
locClient.setLocOption(option);
locClient.registerLocationListener(this);
locClient.start();
自定義搜尋方法
// 查詢周圍2000米的某類建築物
public void poiSearch(String keyWords) {
// 得到目前所在位置坐标
BDLocation location = locClient.getLastKnownLocation();
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
// 成功執行後将回調OnGetPoiSearchResultListener的方法
mPoiSearch.searchNearby(new PoiNearbySearchOption().location(ll)
.keyword(keyWords).radius());
}
2、 editText搜尋監聽回調方法
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
// 點選搜尋後,自動隐藏軟鍵盤
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(arg0.getWindowToken(), );
// 調用搜尋方法
poiSearch(arg0.getText().toString());
return true;
}
3、OnGetPoiSearchResultListener回調方法
@Override
public void onGetPoiDetailResult(PoiDetailResult result) {
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
// 詳情檢索失敗
// result.error請參考SearchResult.ERRORNO
} else {
// 檢索成功
}
}
@Override
public void onGetPoiResult(PoiResult result) {
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
baiduMap.clear();
// 自定義MyPoiOverlay繼承自PoiOverlay
PoiOverlay overlay = new MyPoiOverlay(baiduMap);
// 設定overlay可以處理标注點選事件
baiduMap.setOnMarkerClickListener(overlay);
// 設定PoiOverlay資料
overlay.setData(result);
overlay.addToMap();
overlay.zoomToSpan();
return;
}
}
自定義的MyPoiOverlay
private class MyPoiOverlay extends PoiOverlay {
// 用來存儲位置資訊的類
PoiInfo poi;
public MyPoiOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
// 點選marker觸發的事件
@Override
public boolean onPoiClick(int index) {
poi = getPoiResult().getAllPoi().get(index);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MainActivity.this);
// 設定Title的内容
alertDialog.setTitle("位置資訊");
// 設定顯示資訊
alertDialog.setMessage(poi.name + "\n" + poi.address);
// 設定一個NegativeButton
alertDialog.setNegativeButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
return true;
}
}
4、注意事項
以下方法必須重寫
@Override
public void onDestroy() {
locClient.stop();
baiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mPoiSearch.destroy();
mMapView = null;
Log.d("-d", "mapview");
super.onDestroy();
}
@Override
public void onResume() {
// 在activity執行onResume時執行mMapView. onResume (),實作地圖生命周期管理
mMapView.onResume();
super.onDestroy();
}
@Override
public void onPause() {
// 在activity執行onPause時執行mMapView. onPause (),實作地圖生命周期管理
mMapView.onPause();
super.onPause();
}
下篇将實作百度地圖路徑規劃
代碼下載下傳
如果感覺了解起來比較困難,可以參考百度地圖開發指南