我正在使用 Google Maps Android Marker Clustering Utility,我想更改地图上的标记位置。为此,我已经覆盖DefaultClusterRenderer
并将其设置为我的ClusterManager
. 问题是,每当我更新集群管理器项或删除并再次添加它时,onClusterItemUpdated
我的渲染器中的方法永远不会被调用。我希望它被调用的原因 - 它为Marker
实例提供了我想要更改的位置。
所以,我想知道如何调用该onClusterItemUpdated
方法。
我的代码如下。
注意:当标记在缩小地图时聚集在一起并在缩小标记时再次分离时,将获取所需的更新位置。
我的渲染器
public class MyClusterRenderer extends DefaultClusterRenderer<MyClusterItem> {
private GoogleMap map;
public MyClusterRenderer(Context context, GoogleMap map, ClusterManager<MyClusterItem> clusterManager) {
super(context, map, clusterManager);
this.setMinClusterSize(2);
this.map = map;
}
@Override
protected void onBeforeClusterItemRendered(MyClusterItem myClusterItem, MarkerOptions markerOptions) {
markerOptions
.icon(myClusterItem.getIcon())
.title(myClusterItem.getTitle());
}
@Override
protected void onClusterItemUpdated(MyClusterItem myClusterItem, Marker marker) {
super.onClusterItemUpdated(myClusterItem, marker);
if (!marker.getPosition().equals(myClusterItem.getPosition())) {
marker.setPosition(myClusterItem.getPosition());
}
marker.setIcon(myClusterItem.getIcon());
marker.setTitle(myClusterItem.getTitle());
}
@Override
protected void onBeforeClusterRendered(Cluster<MyClusterItem> cluster, MarkerOptions markerOptions) {
super.onBeforeClusterRendered(cluster, markerOptions);
}
@Override
protected void onClusterUpdated(Cluster<MyClusterItem> cluster, Marker marker) {
super.onClusterUpdated(cluster, marker);
}
}
我的集群项目
public class MyClusterItem implements ClusterItem {
private LatLng position;
private final String title;
private final String snippet;
private BitmapDescriptor icon;
private String friendId;
public MyClusterItem(LatLng latlng, String title, String snippet, BitmapDescriptor icon, String friendId) {
this.position = latlng;
this.title = title;
this.snippet = snippet;
this.icon = icon;
this.friendId = friendId;
}
/// HERE GETTERS AND SETTERS///
}
在我的片段中使用标记聚类
private void setClusterManager(){
clusterManager = new ClusterManager<MyClusterItem>(getActivity(), mMap);
clusterManager.setRenderer(new MyClusterRenderer(getActivity(), mMap, clusterManager));
clusterManager.setOnClusterItemClickListener(this);
mMap.setOnCameraIdleListener(clusterManager);
}
//--------//
private void putClusterItem(){
MyClusterItem clusterItem = new MyClusterItem(latLng, "Title", "Snippet", icon, friendId);
clusterManager.addItem(clusterItem);
}
//--------//
private void updateClusterItem(){
boolean updated = clusterManager.updateItem(clusterItem); // It gives true, but onClusterItemUpdated() is not called
Log.d("updateItem", "Updated: " + updated);
clusterManager.cluster();
}