androidandroid-maps-v2android-maps-utils

How to temporarily disable map marker clustering?


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?


Solution

  • 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);