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餐厅
-
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
-
{
-
-
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();
-
mapController.setZoom(12);
-
-
-
MKSearch mKSearch = new MKSearch();
-
mKSearch.init(mapManager, new MySearchListener());
-
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();
-
}
-
}