Search code examples
androidgoogle-mapsmarkerclusterer

How to modify cluster item total sum value?


Currently I'm using google maps utils to implement markers and their clustering in the map.

The question is that I want only some of those markers to count for the total sum of the cluster density.

I was looking to make the decision process based on the markers tag, but I can't seem to find no way to alter the total marker count in each cluster.

Is there any way to do this?

Thank you


Solution

  • In your ClusterRenderer override onBeforeClusterRendered as follows:

    @Override
    protected void onBeforeClusterRendered(Cluster<MyClusterItem> cluster,
                                           MarkerOptions markerOptions) {
    
        // Calculate totalSum by checking the color of item
        int totalSum = 0;
        for (MyClusterItem item : cluster) {
            // Assume each MyClusterItem holds an attribute -- String color --.
            // Also, assume you can get this attribute by using the following method.
            if (item.getColorAsString().equals("red")) {
                totalSum++;
            }
        }
    
        // Write a method to customize your cluster drawable according to totalSum.
        Drawable customDrawable = customizeClusterDrawable(totalSum);
        mClusterImageView.setImageDrawable(customDrawable);
        Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
    }