前段時間用到和學習了百度地圖,很早就像總結一下,可是太懶,拖到了新年的最後一天,
百度地圖3.0相較于之前的API有所差差别,先導入給出Demo
申請key,添加架包在百度地圖API中都可以找到,這裡就不啰嗦了
我認為3.0的不用就是擷取地圖管理器的方法不同了,也不需要必須在setContentView方法之前初始化管理器
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
在Activity結束時,mapView需要為空
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
CloudManager.getInstance().destroy();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
下面是一些基本配置
// 隐藏地圖縮放按鈕
mMapView.showZoomControls(false);
// 準備辨別位址圖示
descriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.map_store_mark));
// 擷取地圖管理器
mBaiduMap = mMapView.getMap();
// 普通地圖模式
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
// 設定地圖最初縮放比例
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.0f);
// 設定地圖顯示坐标
MapStatusUpdate newLatLng = MapStatusUpdateFactory.newLatLng(new LatLng(Double.valueOf(latitude), Double
.valueOf(longitude)));
mBaiduMap.setMapStatus(msu);
mBaiduMap.setMapStatus(newLatLng);
顯示辨別圖層
// add marker overlay
LatLng latLng = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
OverlayOptions oo = new MarkerOptions().position(latLng).icon(descriptor).zIndex(9).draggable(true);
mMarker = (Marker) (mBaiduMap.addOverlay(oo));
Button button = new Button(getApplicationContext());
button.setBackgroundResource(R.drawable.map_text_bg);
button.setText(name + "\n" + address + "\n" + workingTime);
button.setTextColor(getResources().getColor(R.color.white));
LatLng ll = mMarker.getPosition();
mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button), ll, -47, null);
mBaiduMap.showInfoWindow(mInfoWindow);
定位
// 定位圖層顯示模式:普通
mCurrentMode = LocationMode.NORMAL;
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, null));
// 開啟定位圖層
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(new MyLocationListenner());
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打開gps
option.setCoorType("bd09ll"); // 設定坐标類型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
定位監聽
/**
* 定位SDK監聽函數
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 銷毀後不在處理新接收的位置
if (location == null || mMapView == null)
return;
MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
// 此處設定開發者擷取到的方向資訊,順時針0-360
.direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
startLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Log.i("--------------定位Latitude=" + location.getLatitude());
Log.i("--------------定位Longitude=" + location.getLongitude());
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
初始化導航引擎
BaiduNaviManager.getInstance().initEngine(NearShopMapActivity.this, getSdcardDir(), mNaviEngineInitListener,
new LBSAuthManagerListener() {
@Override
public void onAuthResult(int status, String msg) {
String str = null;
if (0 == status) {
str = "key校驗成功!";
} else {
str = "key校驗失敗, " + msg;
}
// Toast.makeText(NearShopMapActivity.this, str,
// Toast.LENGTH_SHORT).show();
Log.i("==========導航初始化=====" + str);
}
});
開啟導航(調用百度地圖APP或者使用導航SDK)
endLatLng = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
// 建構 導航參數
NaviPara para = new NaviPara();
para.startPoint = startLatLng;
para.startName = "從這裡開始";
para.endPoint = endLatLng;
para.endName = "到這裡結束";
if (isAvilible("com.baidu.BaiduMap")) {
try {
BaiduMapNavigation.openBaiduMapNavi(para, NearShopMapActivity.this);
} catch (BaiduMapAppNotSupportNaviException e) {
e.printStackTrace();
// AlertDialog.Builder builder = new
// AlertDialog.Builder(NearShopMapActivity.this);
// builder.setMessage("您尚未安裝百度地圖app或app版本過低,點選确認安裝?");
// builder.setTitle("提示");
// builder.setPositiveButton("取消", null);
// builder.setNegativeButton("确定", new
// DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int
// which) {
// dialog.dismiss();
// BaiduMapNavigation.getLatestBaiduMapApp(NearShopMapActivity.this);
// }
// });
// builder.create().show();
}
} else {
mStartPoint = new BNaviPoint(startLatLng.longitude, startLatLng.latitude, "起點",
BNaviPoint.CoordinateType.GCJ02);
mEndPoint = new BNaviPoint(endLatLng.longitude, endLatLng.latitude, "終點",
BNaviPoint.CoordinateType.GCJ02);
mViaPoints = new ArrayList<BNaviPoint>();
if (mViaPoints.size() == 0) {
launchNavigator();
} else {
launchNavigatorViaPoints();
}
}
使用導航SDK
/**
* 啟動GPS導航. 前置條件:導航引擎初始化成功
*/
private void launchNavigator() {
// 這裡給出一個起終點示例,實際應用中可以通過POI檢索、外部POI來源等方式擷取起終點坐标
BaiduNaviManager.getInstance().launchNavigator(NearShopMapActivity.this, startLatLng.latitude,
startLatLng.longitude, "起點", endLatLng.latitude, endLatLng.longitude, "終點",
NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, // 算路方式
true, // 真實導航
BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, // 在離線政策
new OnStartNavigationListener() { // 跳轉監聽
@Override
public void onJumpToNavigator(Bundle configParams) {
Intent intent = new Intent(NearShopMapActivity.this, BNavigatorActivity.class);
intent.putExtras(configParams);
startActivity(intent);
}
@Override
public void onJumpToDownloader() {
}
});
}