Android cluster and marker clicks

tio oit picture tio oit · Oct 1, 2015 · Viewed 10.4k times · Source

I'm using the android clustering utility on a map and succesffuly implemented an onclick listener with mClusterManager.setOnClusterItemClickListener() for all the markers handled by the clustering library.

Because I also want some markers to always be unclustered, I also add some markers without using the mClusterManager but directly using the map's mMap.addMarker(), this way I'm sure that they are never clustered on the map.

My problem is that I cannot intercept clicks on those markers (the always unclustered ones) because I already used mMap.setOnMarkerClickListener(mClusterManager) to handle the clicked clusters' markers.

Is there a way to listen to the clicked clustered markers AND the clicked markers not handled by the clustering library ?

Or is there a way to specify the cluster manager to never cluster some markers ? In this case I won't have to handle those different click listener since all markers would be shown using the clustering utility.

Thank you

Answer

NasaGeek picture NasaGeek · Oct 1, 2015

You can create a new MarkerManager that you pass into the ClusterManager constructor. Then make a new Marker collection using MarkerManager#newCollection and add your normal Markers to the map using the MarkerManager.Collection#addMarker method.

Then, instead of calling mMap.setOnMarkerClickListener(mClusterManager), call mMap.setOnMarkerClickListener(mMarkerManager), and it will handle forwarding your Marker click events to the proper listeners. You'll also need to set up your onMarkerClick listener for normal Markers with the MarkerManager.Collection#setOnMarkerClickListener(GoogleMap.OnMarkerClickListener markerClickListener) function.

I recommend looking over the source of the MarkerManager and ClusterManager classes to get a better idea of how the classes interact.