POI搜索有三種方式,根據范圍和檢索詞發起范圍檢索poiSearchInbounds,城市poi檢索poiSearchInCity,周邊檢索poiSearchNearBy。
下以周邊檢索為例介紹如何進行檢索並顯示覆蓋物PoiOverlay:
public int poiSearchNearBy(java.lang.String key, GeoPoint pt, int radius)
-
根據中心點、半徑與檢索詞發起周邊檢索.
-
異步函數,返回結果在MKSearchListener裡的onGetPoiResult方法通知
-
參數:
-
key - 關鍵詞
-
pt - 中心點地理坐標
-
radius - 半徑,單位:米
-
返回:
-
成功返回0,否則返回-1 Demo: 檢索天安門周邊5000米之內的KFC餐廳
[java]
- mMKSearch.poiSearchNearBy(KFC, new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);
實現MySearchListener的onGetPoiResult,並展示檢索結果:
[java]
- public void onGetPoiResult(MKPoiResult result, int type, int iError) {
- if (result == null) {
- return;
- }
- PoiOverlay poioverlay = new PoiOverlay(MyMapActivity.this, mMapView);
- poioverlay.setData(result.getAllPoi());
- mMapView.getOverlays().add(poioverlay);
- }
具體實現: [java]
- package xiaosi.baiduMap;
-
- import android.os.Bundle;
-
- import com.baidu.mapapi.BMapManager;
- import com.baidu.mapapi.GeoPoint;
- import com.baidu.mapapi.MKAddrInfo;
- import com.baidu.mapapi.MKDrivingRouteResult;
- import com.baidu.mapapi.MKPoiResult;
- import com.baidu.mapapi.MKSearch;
- import com.baidu.mapapi.MKSearchListener;
- import com.baidu.mapapi.MKTransitRouteResult;
- import com.baidu.mapapi.MKWalkingRouteResult;
- import com.baidu.mapapi.MapActivity;
- import com.baidu.mapapi.MapController;
- import com.baidu.mapapi.MapView;
- import com.baidu.mapapi.PoiOverlay;
-
- public class BaiduMapActivity extends MapActivity
- {
- /** Called when the activity is first created. */
- private BMapManager mapManager = null;
- private String key = 1B79478DA01F7800AEA8602517A6D89B38151105;
- private MapView mapView = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mapManager = new BMapManager(getApplication());
- mapManager.init(key, null);
- super.initMapActivity(mapManager);
- mapView = (MapView) findViewById(R.id.mapsView);
- mapView.setBuiltInZoomControls(true); // 設置啟用內置的縮放控件
- MapController mapController = mapView.getController(); // 得到mMapView的控制權,可以用它控制和驅動平移和縮放
- mapController.setZoom(12); // 設置地圖zoom級別
-
-
- MKSearch mKSearch = new MKSearch();
- mKSearch.init(mapManager, new MySearchListener());// 注意,MKSearchListener只支持一個,以最後一次設置為准
- mKSearch.poiSearchNearBy(KFC, new GeoPoint((int) (39.915 * 1E6),
- (int) (116.404 * 1E6)), 5000);
- }
-
- public class MySearchListener implements MKSearchListener
- {
- public void onGetAddrResult(MKAddrInfo arg0, int arg1)
- {}
-
- public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
- {}
-
- public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
- {
- if (arg0 == null) {
- return;
- }
- PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this, mapView);
- poioverlay.setData(arg0.getAllPoi());
- mapView.getOverlays().add(poioverlay);
- }
-
- public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
- {}
-
- public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
- {}
- }
-
- @Override
- protected boolean isRouteDisplayed()
- {
- return false;
- }
-
- @Override
- protected void onDestroy()
- {
- if (mapManager != null)
- {
- mapManager.destroy();
- mapManager = null;
- }
- super.onDestroy();
- }
-
- @Override
- protected void onPause()
- {
- if (mapManager != null)
- {
- mapManager.stop();
- }
- super.onPause();
- }
-
- @Override
- protected void onResume()
- {
- if (mapManager != null)
- {
- mapManager.start();
- }
- super.onResume();
- }
- }