Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 327
Arūnas Bedžinskas Avatar asked Dec 03 '14 14:12

Arūnas Bedžinskas


2 Answers

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);
like image 111
Arūnas Bedžinskas Avatar answered Nov 10 '22 07:11

Arūnas Bedžinskas


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.

like image 2
ian.shaun.thomas Avatar answered Nov 10 '22 07:11

ian.shaun.thomas