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:

  1. LatLng getPosition()
  2. String getTitle()
  3. 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 infowindows, we have to create custom renderer class as well which will extend DefaultClusterRenderer class.

 

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 clustermanager in the onMapReady callback.

 

@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.

 

 

 

 

About Author

Author Image
Rahul Baboria

Rahul Baboria is having good knowledge over Android Application.

Request for Proposal

Name is required

Comment is required

Sending message..