鉴于百度地图不计其数的poi兴趣点 很多app在开发的时候都会选择调用百度地图资源
//主界面Activity
package com.zhuang.search;
import java.util.ArrayList;
import java.util.List;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.OverlayManager;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.core.SearchResult;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity implements OnGetPoiSearchResultListener{
/** * POI周边检索 */
private PoiSearch Mpoisearch;
private MapView mapView;
private BaiduMap Mmap;
private double latitude,longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//开始poi检索
//参数 keyword("关键字").radius(半径).location(new LatLng(坐标)))
Mpoisearch.searchNearby(new PoiNearbySearchOption().keyword("养生").radius(5000).location(new LatLng(latitude,longitude)));
}
private void initView() {
mapView = (MapView) findViewById(R.id.bmapView);
Mmap = mapView.getMap();
latitude = Double.parseDouble(SPUtils.getString(MainActivity.this, "latitude",""));
longitude = Double.parseDouble(SPUtils.getString(MainActivity.this, "longitude",""));
//获取poi检索实例
Mpoisearch = PoiSearch.newInstance();
//实现检索监听
Mpoisearch.setOnGetPoiSearchResultListener(this);
}
/********************* POI检索结果的方法 ***********************/
public void onGetPoiResult(PoiResult result) {
if (result == null|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(MainActivity.this, "未找到结果", Toast.LENGTH_LONG).show();
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
//清除数据
Mmap.clear();
//添加poi覆盖物 并添加覆盖物的监听事件
MyPoiOverlay overlay = new MyPoiOverlay(Mmap);
Mmap.setOnMarkerClickListener(overlay);
overlay.setResult(result);
overlay.addToMap();
overlay.zoomToSpan();
return;
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
// 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
String strInfo = "在";
for (CityInfo cityInfo : result.getSuggestCityList()) {
strInfo += cityInfo.city;
strInfo += ",";
}
strInfo += "找到结果";
Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_LONG).show();
}
}
public void onGetPoiDetailResult(PoiDetailResult result) {
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(MainActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, result.getName() + ":" + result.getAddress(), Toast.LENGTH_SHORT).show();
}
}
//自定义poi检索覆盖物
private class MyPoiOverlay extends OverlayManager {
private PoiResult result;
private boolean flag = false;
public void setResult(PoiResult result) {
this.result = result;
}
public MyPoiOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public boolean onMarkerClick(Marker marker) {
onClick(marker.getZIndex());
return true;
}
public boolean onClick(int index) {
PoiInfo poi = result.getAllPoi().get(index);
//if(poi.hasCaterDetails){
Mpoisearch.searchPoiDetail((new PoiDetailSearchOption()).poiUid(poi.uid));
// }
return true;
}
@Override
public boolean onPolylineClick(Polyline arg0) {
return false;
}
@Override
public List<OverlayOptions> getOverlayOptions() {
List<OverlayOptions> ops = new ArrayList<OverlayOptions>();
List<PoiInfo> pois = result.getAllPoi();
OverlayOptions op = null;
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_green);
BitmapDescriptor bitmap2 = BitmapDescriptorFactory.fromResource(R.drawable.icon_red);
for (int i = 0; i < pois.size(); i++) {
if(flag){
op = new MarkerOptions().position(pois.get(i).location).icon(bitmap);
}
else{
op = new MarkerOptions().position(pois.get(i).location).icon(bitmap2);
}
ops.add(op);
Mmap.addOverlay(op).setZIndex(i);
}
return ops;
}
}
@Override
protected void onPause() {
mapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
MyApp app = (MyApp) getApplication();
app.mLocClient.stop();
mapView.onDestroy();
mapView = null;
super.onDestroy();
}
}
//Application
package com.zhuang.search;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import android.app.Application;
public class MyApp extends Application {
public LocationClient mLocClient;
@Override
public void onCreate() {
super.onCreate();
//初始化百度地图
SDKInitializer.initialize(getApplicationContext());
// 开始定位
// 创建定位客户端
mLocClient = new LocationClient(getApplicationContext());
// 注册定位的监听,获取定位的结果
mLocClient.registerLocationListener(new BDLocationListener() {
// 获取定位结果
@Override
public void onReceiveLocation(BDLocation location) {
//将定位的经纬度存入SharedPreferences
// 纬度
SPUtils.putString(getApplicationContext(), "latitude", String.valueOf(location.getLatitude()));
// 经度
SPUtils.putString(getApplicationContext(), "longitude", String.valueOf(location.getLongitude()));
}
});
// 对客户端进行各种配置
// 创建定位客户端的选项
LocationClientOption options = new LocationClientOption();
options.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
options.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
options.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
options.setIsNeedAddress(true);// 返回的定位结果包含地址信息
options.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
mLocClient.setLocOption(options);
// 开始定位
mLocClient.start();
}
}
//sp工具类
package com.zhuang.search;
import android.content.Context;
import android.content.SharedPreferences;
public class SPUtils {
private static SharedPreferences sharedPreferences;
// 存double(上下文,key,value)
public static void putString(Context context, String key, String value) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences("config",Context.MODE_PRIVATE);
}
sharedPreferences.edit().putString(key, value).commit();
}
// 取
public static String getString(Context context, String key, String defValue) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences("config",Context.MODE_PRIVATE);
}
return sharedPreferences.getString(key, defValue);
}
}