/**
* 演示poi搜索功能
*/
public class PoiSearchDemo extends Activity {
/** 百度地图 */
private MapView mapView;
/** 定义 BaiduMap 地图对象的操作方法与接口 */
private BaiduMap baiduMap;
private PoiSearch poiSearch;
/** 搜索关键词 */
private final String keyword = "餐厅";
/** 每页容量50 */
private final int pageCapacity = 50;
/** 第一页 */
private final int pageNum = 0;
/** 搜索半径10km */
private final int radius = 10000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
initView();
initData();
}
private void initView() {
mapView = (MapView) findViewById(R.id.mapView);
// 是否显示自带缩放控件
mapView.showZoomControls(false);
}
private void initData() {
baiduMap = mapView.getMap();
// 初始化搜索模块
poiSearch = PoiSearch.newInstance();
// 注册搜索事件监听
poiSearch
.setOnGetPoiSearchResultListener(new PoiSearchResultListener());
// 搜索该坐标附近的餐厅
poiSearch.searchNearby(new PoiNearbySearchOption().keyword(keyword)
.location(new LatLng(39.904965, 116.327764))
.pageCapacity(pageCapacity).pageNum(pageNum).radius(radius));
}
@Override
protected void onPause() {
mapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
// 销毁搜索模块
poiSearch.destroy();
// 关闭定位图层
baiduMap.setMyLocationEnabled(false);
mapView.onDestroy();
mapView = null;
super.onDestroy();
}
private class PoiSearchResultListener implements
OnGetPoiSearchResultListener {
@Override
public void onGetPoiDetailResult(PoiDetailResult result) {
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
} else {
Toast.makeText(getApplicationContext(),
result.getName() + ": " + result.getAddress(), 0)
.show();
}
}
@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 overlay = new MyPoiOverlay(baiduMap);
baiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result);
overlay.addToMap();
// 缩放地图,使所有Overlay都在合适的视野内
overlay.zoomToSpan();
return;
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
}
}
}
private class MyPoiOverlay extends OverlayManager {...}
}
这里与官方的demo很相似,不同的是MyPoiOverlay这个类