0:修改Scan为0,就是只执行一次。
options.setScanSpan(0);
1:在onCreate方法和onDestroy 里面进行初始化和销毁。
private PoiSearch mPoiSearch;
onCreate()-->poiSearch = PoiSearch.newInstance();
onDestory()--> poiSearch.destroy();
@Override protected void onCreate(Bundle savedInstanceState) {
。。。 mPoiSearch=PoiSearch.newInstance(); }
@Override protected void onDestroy() { 。。。 mPoiSearch.destroy(); }
2:添加一个button,
布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <com.baidu.mapapi.map.MapView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/mapView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnFindFoodNearby" android:text="搜寻周围的美食"/> </RelativeLayout>
public void btnFindFoodNearby(View view) { if(mMarker!=null){//这里的mMarker,是用来标记中心位置的,之前的代码里面写过。 PoiNearbySearchOption option = new PoiNearbySearchOption(); option.location(mMarker.getPosition()); option.keyword("美食").radius(1000);//半径500,1000,2000 mPoiSearch.searchNearby(option); } }
3:在onCreat 方法里面注册监听器
mPoiSearch.setOnGetPoiSearchResultListener(this);
4:回调方法:
@Override public void onGetPoiResult(PoiResult poiResult) { } @Override public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) { }
5:
@Override public void onGetPoiResult(PoiResult poiResult) { //结果包含在poi中 List<PoiInfo> allPoi = poiResult.getAllPoi(); if (allPoi != null) { for(PoiInfo poi:allPoi){ LatLng location = poi.location; String name = poi.name; String address = poi.address; MarkerOptions options = new MarkerOptions(); options.position(location).title(name); //BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher); BitmapDescriptor icon = BitmapDescriptorFactory.fromAsset("Icon_mark1.png"); options.icon(icon); Bundle bundle = new Bundle(); bundle.putString("address",address); options.extraInfo(bundle); mMap.addOverlay(options); } } }
++++++++++++++++++++++++++++++++++++++++++++++
参考:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
如果我要查询当前500米以内的美食,公交站台。
定位里面的:Poi point of interesting。
地图里面的:的Poi,http://wiki.lbsyun.baidu.com/cms/androidsdk/doc/v3_6_1/
PoiSearch
//用于搜索周边信息的
private PoiSearch poiSearch;
onCreate()-->poiSearch = PoiSearch.newInstance();
onDestory()--> poiSearch.destroy();
public void btnFindFoodNearby(View view) {
//TODO: 查找周边的美食
//需要使用PoiSearch引擎,最好是一个成员变量,onCreate 去初始化。onDestory销毁
//查找周边
if(currentLocationMarker!=null) {
PoiNearbySearchOption option = new PoiNearbySearchOption();
option.location(currentLocationMarker.getPosition());
option.keyword("美食").radius(1000);//半径500,1000,2000
poiSearch.searchNearby(option);
}
}
onCreat 里面设置poiSearch.setOnGetPoiSearchResultListener(this);
继承
//poi结果回调
@Override
public void onGetPoiResult(PoiResult poiResult) {
//结果包含在poi中
List<PoiInfo> allPoi = poiResult.getAllPoi();
if (allPoi != null) {
for(PoiInfo poi:allPoi){
LatLng location = poi.location;
String name = poi.name;
String address = poi.address;
MarkerOptions options = new MarkerOptions();
options.position(location).title(name);
//BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher);
BitmapDescriptor icon = BitmapDescriptorFactory.fromAsset("Icon_mark1.png");
options.icon(icon);
Bundle bundle = new Bundle();
bundle.putString("address",address);
options.extraInfo(bundle);
map.addOverlay(options);
}
}
}