1.设置地图中心点
private void setMapCenter(double latitude, double longitude) {
dw_pt = new LatLng(latitude, longitude); //设定中心点坐标
MapStatus mMapStatus = new MapStatus.Builder()//定义地图状态
.target(dw_pt)
.zoom(15)
.build(); //定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.setMapStatus(mMapStatusUpdate);//改变地图状态
}
2.捕获地图单击事件
mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
@Override
public void onMapClick(LatLng arg0) {
mBaiduMap.hideInfoWindow();
}
});
3.地图状态改变监听
mBaiduMap.setOnMapStatusChangeListener(statusListener);
BaiduMap.OnMapStatusChangeListener statusListener = new BaiduMap.OnMapStatusChangeListener() {
/**
* 手势操作地图,设置地图状态等操作导致地图状态开始改变。
*
* @param status
* 地图状态改变开始时的地图状态
*/
public void onMapStatusChangeStart(MapStatus status) {
}
@Override
public void onMapStatusChangeStart(MapStatus mapStatus, int i) {
}
/**
* 地图状态变化中
*
* @param status
* 当前地图状态
*/
public void onMapStatusChange(MapStatus status) {
}
/**
* 地图状态改变结束
*
* @param status
* 地图状态改变结束后的地图状态
*/
public void onMapStatusChangeFinish(MapStatus status) {
String latlngStr = status.bound.getCenter().latitude + "," + status.bound.getCenter().longitude;
String zoomStr = (int)status.zoom + "";
requestLedMapCommand(zoomStr, latlngStr);
}
};
4,添加Marker
private void setData() {
if (transportInvoiceListModel != null && transportInvoiceListModel.result != null) {
for (Marker mm : markerList) {
mm.remove();
}
markerList.clear();
//mBaiduMap.hideInfoWindow();
for(TransportInvoice invoice : transportInvoiceListModel.result){
// 定义Maker坐标点
LatLng point = new LatLng(Utils.toDouble(invoice.nowLatitude), Utils.toDouble(invoice.nowLongitude));
// 构建Marker图标
BitmapDescriptor bd = BitmapDescriptorFactory
.fromResource(R.mipmap.fx_icon_sf);
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option1 = new MarkerOptions().position(point).icon(
bd);
// 在地图上添加Marker,并显示
Marker marker = (Marker) mBaiduMap.addOverlay(option1);
markerList.add(marker);
}
}
}
5.Marker单击弹出InfoWindow
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
if(transportInvoiceListModel != null && transportInvoiceListModel.result != null) {
if (last_marker != null) {
BitmapDescriptor bd = BitmapDescriptorFactory.fromResource(R.mipmap.fx_icon_sf);
last_marker.setIcon(bd);
mBaiduMap.hideInfoWindow();
}
int i = 0;
for (Marker mm : markerList) {
if (mm.equals(marker)) {
final TransportInvoice www = transportInvoiceListModel.result.get(i);
BitmapDescriptor bd = BitmapDescriptorFactory
.fromResource(R.mipmap.fx_icon_sf);
mm.setIcon(bd);
final LatLng ll = mm.getPosition();
final InfoWindow.OnInfoWindowClickListener listener = new InfoWindow.OnInfoWindowClickListener() {
public void onInfoWindowClick() {
requestLedCommand(www.id, "2");
}
};
final View contentView = Finder.inflate(DriversLocationActivity.this,
R.layout.map_pop_driver);
TextView name_tv = (TextView) contentView.findViewById(R.id.name_tv);
TextView phone_tv = (TextView) contentView.findViewById(R.id.phone_tv);
name_tv.setText(www.driverName);
phone_tv.setText(www.phone);
mInfoWindow = new InfoWindow(BitmapDescriptorFactory
.fromView(contentView), ll, -47, listener);
mBaiduMap.showInfoWindow(mInfoWindow);
last_marker = mm;
}
i++;
}
}
return true;
}
});
initTimer();
}