Search code examples
androidandroid-mapsandroid-maps-utils

Google Maps Clustering not working with dynamic data


I have a problem with the clustering of GoogleMaps. In my app im updating the map when the User zooms in or scrolls over the Map. Im using the OnCameraIdleListener (link) for this:

@Override
public void onCameraIdle() {
   clearItems();
   do API Call
}

When this happens im doing an API call with the new radius and im receiving a new list of objects that are visible inside my current radius. After i've received the data im drawing the new POIs to my map. Therefore i iterate over a list and for each element i create a cluster item and add it to the Cluster Manager.

CustomClusterItem clusterItem = new CustomClusterItem(new LatLng(item.getLocation().getLatitude(), item.getLocation().getLongitude()), poi); 
mClusterManager.addItem(clusterItem);

and after the iteration im calling

mClusterManager.cluster();

Sadly the clustering does not work. It doesn't matter how far im zooming out of the map for example. Is there something im missing?

Thanks in advance!


Solution

  • I just had 4 POIs on my Map and the Clustering just starts with 5 items. So there is no mistake. I just didn't know that there is a size configured by default. If someone wants to achieve clustering with less then 5 items you can use

    @Override
    protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
        //start clustering if at least 2 items overlap
        return cluster.getSize() > 1;
    }
    

    this method from the DefaultClusterRenderer (https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/clustering/view/DefaultClusterRenderer.java).