在配置好百度地圖的使用權限和key值之后,繪制地圖覆蓋物步驟:
1、初始化地圖
/**初始化百度地圖
*
*/
private void initBaiduMap(){
//初始化地圖
mMapView = (MapView) findViewById(R.id.map);
mMapView.showZoomControls(false);//縮放按鈕
mBaidumap = mMapView.getMap();
//地圖點擊事件處理
mBaidumap.setOnMapClickListener(this);
// 初始化搜索模塊,注冊事件監(jiān)聽
mSearch = RoutePlanSearch.newInstance();
mSearch.setOnGetRoutePlanResultListener(this);
// mBaidumap.setOnMapLoadedCallback(new OnMapLoadedCallback() {
// //地圖加載完成回調,該方法有時沒有返回,原因不明,還在研究,讀者有經驗可以交流一下
// @Override
// public void onMapLoaded() {
// // TODO Auto-generated method stub
// ToastUtils.showTextToast(SelectStationActivity.this, "地圖加載完成");
//
// }
// });
}
2、添加覆蓋物
BitmapDescriptor bdC = BitmapDescriptorFactory.fromView(view);
/*
此處BitmapDescriptorFactory.fromView(view);采用的是自定義覆蓋物
view = View.inflate(getApplicationContext(), R.layout.view_baidumap, null);
也可以使用BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)
BitmapDescriptorFactory.fromBitmap(bitmap)
*/
LatLng ll = new LatLng(stations.get(i).getLatitude(), stations.get(i).getLongitude());
MarkerOptions ooC = null;
if (overlayIconCenter) {//true 居中對齊
ooC = new MarkerOptions().position(ll)//經緯度
.icon(bdC)//覆蓋物的icon,可以選擇icons(ArrayList<BitmapDescriptor>)多個icon實現輪播動畫效果
.perspective(false)
.anchor(0.5f, 1f)//覆蓋物的對齊點,0.5f,0.5f為覆蓋物的中心點
.zIndex(i);
}else{//左對齊
ooC = new MarkerOptions().position(ll).icon(bdC)
.perspective(false).anchor(0f, 1f).zIndex(i);
}
if (animation) {
//生長動畫
ooC.animateType(MarkerAnimateType.grow);//還可以選擇掉落的動畫
}
mBaidumap.addOverlay(ooC);
/*此處可以強轉(Marker) (mBaidumap.addOverlay(ooD));
通過Marker.setPosition(LatLng)控制覆蓋物的位置
*/
3、覆蓋物事件監(jiān)聽
mBaidumap.setOnMapStatusChangeListener(arg0);
mBaidumap.setOnMapDoubleClickListener(arg0);
mBaidumap.setOnMapTouchListener(arg0);
mBaidumap.setOnMapLongClickListener(arg0);
mBaidumap.setOnMarkerClickListener((arg0);
Baidumap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
//可以通過arg0.getZIndex()判斷Marker
return true;
}
});