NO IMAGE

鑑於百度地圖不計其數的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);
}
}