androidgoogle-mapsandroid-maps-utils

How to change cluster image according to the number of marker it included, in Android?


Following image contains different cluster styles according to the size of it. This is exactly what I'm trying to do from Android. I'm using ClusterManager from android-maps-util.

enter image description here

Here's the javascript code that server guy used. If possible, I need the same idea to do so.

gmap.mc.styles_ = [{ url: markersBaseLocation + 'm1.png', height: 53, width: 53 }, { url: markersBaseLocation + 'm2.png', height: 56, width: 57 }, { url: markersBaseLocation + 'm3.png', height: 66, width: 66 }, { url: markersBaseLocation + 'm4.png', height: 78, width: 78 }, { url: markersBaseLocation + 'm5.png', height: 90, width: 90 }];

Solution

  • You can do that by providing your own custom Renderer.

    mClusterManager.setRenderer(new DefaultClusterRenderer<MyItem>(mContext, googleMap, mClusterManager) {
            @Override
            protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions) {
                // use cluster.getSize() to get the number of markers inside the cluster.
            }
    

    The demo code for this can be found in https://github.com/googlemaps/android-maps-utils.git. Look inside "CustomMarkerClusteringDemoActivity.java"