Google Maps is a powerful API by Google, There are some cons about maps such as takes lot of memory, limited customization along with major pros, that is its functionality. If we draw some markers in a normal manner after some level UI will start hanging when there will be thousands of markers. To handle thousand of markers we need to apply clustering which is possible by adding map utils library.
Once it is done, we need to implement ClusterItem Interface in our model class. It will provide needed data of marker to our model.
We have to implement these three methods of ClusterItem:
- LatLng getPosition()
- String getTitle()
- String getSnippet()
Now we need a cluster manager class and have to implement its methods.
Then in the onMapReady method, Initialize the ClusterManager. Take a reference from the code below
In order to do customizations such as changing icons or setting custom
infowindows, we have to create custom renderer class as well which will extend DefaultClusterRenderer class.
In onBeforeClusterItemRendered method is called before rendering default markers. So change the icons like:
It is still incomplete. Now, we need to set this custom renderer to our
clustermanagerin the onMapReady callback.
That's it, ClusterManager setup is done. Further, we can create custom ClusterManager class as well where we can override methods and perform other customizations as well. In CustomClusterRenderer class we can set the icon of the cluster as well in the method onBeforeClusterRendered() method.