ClusterManger In Google Maps Android
Posted By : Rahul Baboria | 29-Mar-2018
Introduction :
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.
Implementation:
'compile com.google.maps.android:android-maps-utils:0.5.0'
Once it is done, we need to implement ClusterItem Interface in our model class. It will provide needed data of marker to our model.
public class CustomItem implements ClusterItem
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.
private ClusterManager<Person> mClusterManager;
Then in the onMapReady method, Initialize the ClusterManager. Take a reference from the code below
mClusterManager = new ClusterManager<>(this, googleMap);
getGoogleMap().setOnCameraIdleListener(mClusterManager);
getGoogleMap().setOnMarkerClickListener(mClusterManager);
getGoogleMap().setOnInfoWindowClickListener(mClusterManager);
addCustomItems();
mClusterManager.cluster();
In order to do customizations such as changing icons or setting custom
public class ClusterRenderer extends DefaultClusterRenderer<ClusterItem> {
private final Context mContext;
public ClusterRenderer(Context context, GoogleMap map,
ClusterManager<ClusterItem> clusterManager) {
super(context, map, clusterManager);
mContext = context;
}
@Override protected void onBeforeClusterItemRendered(ClusterItem item,
MarkerOptions markerOptions) {
}
}
In onBeforeClusterItemRendered method is called before rendering default markers. So change the icons like:
final BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE);
markerOptions.icon(markerDescriptor).snippet(item.title);
It is still incomplete. Now, we need to set this custom renderer to our
@Override public void onMapReady(GoogleMap googleMap) {
// ...
final CustomClusterRenderer renderer = new CustomClusterRenderer(this, mMap, mClusterManager);
mClusterManager.setRenderer(renderer);
}
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.
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Baboria
Rahul Baboria is having good knowledge over Android Application.