Search code examples
javaandroidgoogle-mapsmarkerclusterer

How can I use functions like onClusterItemRendered for my ClusterManager?


I am trying to get a reference to my markers (without using getMarkerCollections) on my clustered Google Map but I am having trouble finding where to override this function.

I have made a typical ClusterManager that works fine, but I can't override the function anywhere in that class.

Do I need to make a custom class for this and how would I do that?


Solution

  • To answer your question directly, you will need to create a custom class that extends DefaultClusterRenderer, and then override the onClusterItemRendered() method:

    public class MyRenderer extends DefaultClusterRenderer<MyItem> {
    
        public MyRenderer(Context context, GoogleMap map, ClusterManager<MyItem> clusterManager) {
            super(context, map, clusterManager);
        }
    
        @Override
        protected void onClusterItemRendered(MyItem clusterItem,
                                             Marker marker) {
            super.onClusterItemRendered(clusterItem, marker);
    
            //other stuff......
        }
    }
    

    Then, you would call the setRenderer() method and give it a new instance of your MyRenderer object:

    mClusterManager.setRenderer(new MyRenderer(this, mMap, mClusterManager));