POI搜索有三种方式,根据范围和检索词发起范围检索poiSearchInbounds,城市poi检索poiSearchInCity,周边检索poiSearchNearBy。
下以周边检索为例介绍如何进行检索并显示覆盖物PoiOverlay:
public int poiSearchNearBy(java.lang.String key, GeoPoint pt, int radius)
-
根据中心点、半径与检索词发起周边检索.
- 异步函数,返回结果在MKSearchListener里的onGetPoiResult方法通知
-
参数:
- key - 关键词
- pt - 中心点地理坐标
- radius - 半径,单位:米
-
返回:
- 成功返回0,否则返回-1
- mMKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);
实现MySearchListener的onGetPoiResult,并展示检索结果:
- 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);
- }
具体实现:
- 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();
- }
- }