1.循环往地图上添加Marker点
2.用Bundle保存每个点的信息,这个实体类需要序列化 继承Serializable 并给一个常量serialVersionUID
private static final long serialVersionUID = 8633299996744734593L;3.setZoomF可以设置地图缩放等级 方便展示所有点
4.Marker的点击监听setOnMarkerClickListener
示例代码:
/**
* 循环在Baidumap上添加marker点,并用bundle保存信息
* 设置marker的点击监听
*/
LatLng latLng = null;
OverlayOptions option = null;
Marker marker = null;
private void initMarker() {
Log.i("marker的头像地址", "《就是这个---》》》》》" + list);
// Log.d("________________", "latLng" + latLng);
Bitmap bitmap1 = getViewBitmap(view);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap1);
for (int i = 0; i < list.size(); i++) {
stationsBean = list.get(i);
latLng = new LatLng(stationsBean.getLatitude(), stationsBean.getLongitude());
//构建MarkerOption,用于在地图上添加Marker
option = new MarkerOptions()
.position(latLng)
.icon(bitmapDescriptor)
.zIndex(9) // 设置marker所在层级
.draggable(true); // 设置手势拖拽
//在地图上添加Marker,并显示
marker = (Marker) mBaidumap.addOverlay(option);
// // 将信息保存
Bundle bundle = new Bundle();
bundle.putSerializable("marker", stationsBean);
marker.setExtraInfo(bundle);
}
myListener.setZoomF(LevelUtil.getLevel(list.get(list.size() - 1).getFarfrom()) + 3);//设置缩放等级 根据点前位置和最远点的距离
LogUtils.MyLog("----等级", LevelUtil.getLevel(list.get(list.size() - 1).getFarfrom()) + "juli" + list.get(list.size() - 1).getFarfrom());
// mBaidumap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(3.0f).build()));
//添加marker点击事件的监听
mBaidumap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//从marker中获取info信息
Bundle bundle = marker.getExtraInfo();
AroundOil.StationsBean stationsBeanThis = (AroundOil.StationsBean) bundle.getSerializable("marker");
LogUtils.MyLog("----点击了", stationsBeanThis.toString());
showMyDialog(stationsBeanThis);
return true;
}
});
}
/**
* 将View转换成Bitmap
*
* @param addViewContent
* @return
*/
private Bitmap getViewBitmap(View addViewContent) {
addViewContent.setDrawingCacheEnabled(true);
addViewContent.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
addViewContent.layout(0, 0,
addViewContent.getMeasuredWidth(),
addViewContent.getMeasuredHeight());
addViewContent.buildDrawingCache();
Bitmap cacheBitmap = addViewContent.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
return bitmap;
}
/**
* 点击marker获得弹窗 调起导航
*
*/
private double d = 0;
private double f1 = 0;
private BigDecimal b = null;
private void showMyDialog(final AroundOil.StationsBean stationsBeanThis) {
//通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象,可以对Dialog的各个参数进行设置
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
//设置Title的标题
builder.setTitle(stationsBeanThis.getOilname());
//设置Title的图标
builder.setIcon(R.mipmap.jiayou);
//设置来显示主要的信息
d = stationsBeanThis.getFarfrom() / 1000;
b = new BigDecimal(d);
f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
builder.setMessage(stationsBeanThis.getAddress() + "\n距离:约" + f1 + "km");
builder.setPositiveButton("导航", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
loadingLayout.setVisibility(View.VISIBLE);
NavUtil.showChoiceNaviWayDialog(getActivity(),new LatLng(latitudenew, longitudenew), new LatLng(stationsBeanThis.getLatitude(), stationsBeanThis.getLongitude()), "当前位置", stationsBeanThis.getAddress());
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(getContext(), "取消" + which,Toast.LENGTH_SHORT).show();
}
});
builder.show();
}