效果如图
请先下载:http://download.csdn.net/download/m18860232520/10166118
自定义ClusterItem类
/**
* 每个Marker点,包含Marker点坐标以及图标
*/
public class MyClusterItem implements ClusterItem {
private final LatLng mPosition;
private RealTimeLocationBean.ListBean listBean;//方向
private View view;//mark布局
public MyClusterItem(RealTimeLocationBean.ListBean listBean) {
mPosition = new LatLng(Double.valueOf(listBean.getBlat()), Double.valueOf(listBean.getBlng()));
this.listBean = listBean;
ViewHolder viewHolder;
view = View.inflate(RealTimeActivity.this, R.layout.mark_layout, null);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
if (Preferences.getBoolean("carNumber", false)) {
viewHolder.textView.setText(listBean.getCarPlate());
} else {
viewHolder.textView.setText(listBean.getCarName());
}
if (Integer.valueOf(listBean.getState()) < 9) {
viewHolder.imageView.setRotation(Float.valueOf(listBean.getDrct()));
} else {
viewHolder.imageView.setRotation(0);
}
viewHolder.imageView.setImageBitmap(ImageUtils.getImageFromAssetsFile("car/" + listBean.getIcon()));
// public static Bitmap getImageFromAssetsFile(String fileName) {
// Bitmap image = null;
// AssetManager am = App.getAppContext().getResources().getAssets();
// try {
// InputStream is = am.open(fileName);
// image = BitmapFactory.decodeStream(is);
// is.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// return image;
// }
//下载到本地进行的加载//
File file = new File(ToolsUtil.imgPath+"/"+listBean.getState()+".png");// if (file.exists()) {// Bitmap bm = BitmapFactory.decodeFile(ToolsUtil.imgPath+"/"+listBean.getState()+".png");// //将图片显示到ImageView中// viewHolder.imageView.setImageBitmap(bm);// } } @Override public LatLng getPosition() { return mPosition; } @Override public String getCarId() { return listBean.getCarId(); } @Override public RealTimeLocationBean.ListBean getListBean() { return listBean; } @Override public BitmapDescriptor getBitmapDescriptor() { return BitmapDescriptorFactory.fromView(view); } class ViewHolder { @BindView(R.id.img_mark_pic) ImageView imageView; @BindView(R.id.tv_mark_name) TextView textView; ViewHolder(View view) { ButterKnife.bind(this, view); } } }
在要进行聚合的类中
private List items = new ArrayList<>();//marker点集合
// 定义点聚合管理类ClusterManager mClusterManager = new ClusterManager<>(this, mBaiduMap);
items.add(new MyClusterItem(bean));
mClusterManager.addItems(items);
//mClusterManager.cluster();//设置后不用移动更新点状态
//mBaiduMap.clear();//清理地图
//items.clear();//清除原来marker的数据
//mClusterManager.clearItems();//清除原来marker的数据
mMapView.showZoomControls(false);//是否显示缩小放大按钮// 设置地图监听,当地图状态发生改变时,进行点聚合运算mBaiduMap.setOnMapStatusChangeListener(mClusterManager);// 设置maker点击时的响应mBaiduMap.setOnMarkerClickListener(mClusterManager);
//集合点展开事件
mClusterManager.setOnClusterClickListener(cluster -> {
List items = (List) cluster.getItems();
LatLngBounds.Builder builder2 = new LatLngBounds.Builder();
for (MyClusterItem MyClusterItem : items) {
builder2 = builder2.include(MyClusterItem.getPosition());
}
LatLngBounds latlngBounds = builder2.build();
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngBounds(latlngBounds, mMapView.getWidth(), mMapView.getHeight());
mBaiduMap.animateMapStatus(u);
return false;
});
//Marker 监听事件
mClusterManager.setOnClusterItemClickListener(item -> {
setMarkPopupWindow(item.getListBean().getCarId(), item.getPosition());
return false;
});
//每次重新设置数据时 设置中心点的方法自动计算 显示地图所有点
RealTimeLocationBean.ListBean//是包含marker点信息的类
private void zoomToSpan(List listBean) {
if (listBean.size() > 0) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (RealTimeLocationBean.ListBean bean : listBean) {
// polyline 中的点可能太多,只按marker 缩放
if (bean.getBlat().equals("") || bean.getBlng().equals("")) continue;
builder.include(new LatLng(Double.valueOf(bean.getBlat()), Double.valueOf(bean.getBlng())));
}
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
}
}
/**
* 重新设置设置点数据
*/
private void setMarkDates(List listBean) {
if (listBean == null || listBean.size() <= 0) return;
mBaiduMap.clear();
items.clear();
mClusterManager.clearItems();
for (RealTimeLocationBean.ListBean bean : listBean) {
if (bean.getBlng().equals("") || bean.getBlat().equals("")) continue;
items.add(new MyClusterItem(bean));
}
mClusterManager.addItems(items);
mClusterManager.cluster();//设置后不用移动更新点状态
zoomToSpan(listBean);
}
//清除全部
imgMapClear.setOnClickListener(view -> {
mBaiduMap.clear();
mClusterManager.clearItems();//清除聚合点
saveListBean.clear();
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(mBaiduMap.getMapStatus()));//刷新地图
});
判断是否聚合if (Preferences.getBoolean("point", true)) {
mClusterManager.setMackSize(2);//聚合起始点2个
} else {
mClusterManager.setMackSize(10000);
}
NonHierarchicalDistanceBasedAlgorithm 下 控制点聚合范围
public static final int MAX_DISTANCE_AT_ZOOM = 200; // essentially 100 dp.设置点聚合距离/**
* ClusterItem represents a marker on the map.
*/
public interface ClusterItem {//接口下方法按需求自行更改
/**
* The position of this marker. This must always return the same value.
*/
LatLng getPosition();//点
String getCarId();//车辆ID
RealTimeLocationBean.ListBean getListBean();//方向
BitmapDescriptor getBitmapDescriptor();//图片
}
1.如果marker点的图片要求加载网络图,而且不进行更改的可以提供思路进入之前先下载图片到本地,然后Bitmap bm = BitmapFactory.decodeFile(ToolsUtil.imgPath+"/"+listBean.getState()+".png");
imageView.setImageBitmap(bm)
进行设置2.如果图片过多,同时需要放在本地又不想通过id获取,建议放在assets 文件夹中,通过以下方法进行获取imageView.setImageBitmap(ImageUtils.getImageFromAssetsFile("car/" + listBean.getIcon()));
public static Bitmap getImageFromAssetsFile(String fileName) {
Bitmap image = null;
AssetManager am = App.getAppContext().getResources().getAssets();
try {
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}