问题
I am using Google Maps V2 for Android together with maps utils extension library for marker clustering. Some parts of app does not need to get clustered markers.
Is there any way to forbid clusterManager to cluster markers and after certain conditions let it cluster items again?
回答1:
I found myself another solution. I figured out that on DefaultClusterRenderer
method shouldRenderAsCluster
is responsible for whether the marker will be rendered as cluster or not. So I created a CustomRenderer class which extends DefaultClusterRenderer
and created a method with a boolean variable to determine whether renderer should cluster or not.
public class CustomRenderer extends DefaultClusterRenderer<MarkerItem>
{
private boolean shouldCluster = true;
private static final int MIN_CLUSTER_SIZE = 1;
//Some code....
public void setMarkersToCluster(boolean toCluster)
{
this.shouldCluster = toCluster;
}
I also override the method here that i mentioned before.
@Override
protected boolean shouldRenderAsCluster(Cluster<MarkerItem> cluster)
{
if (shouldCluster)
{
return cluster.getSize() > MIN_CLUSTER_SIZE;
}
else
{
return shouldCluster;
}
}
And now if I want to stop clustering i just call this method from the activity i want.
ClusterManager clusterManager = new ClusterManager<MarkerItem>(this, googleMap);
CustomRenderer customRenderer = new CustomRenderer(this, googleMap, clusterManager);
clusterManager.setRenderer(customRenderer);
customRenderer.setMarkersToCluster(false);
回答2:
The cluster manager implementation is only capable of performing the built in clustering functionality. If you wish for certain markers to not cluster, you will need to add those markers to the map directly. When you decide to cluster those markers, you will need to remove them from the map and transfer their information to the cluster manager for it to take over.
来源:https://stackoverflow.com/questions/27273857/how-to-temporarily-disable-map-marker-clustering