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
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.
One more way to receive the click event for Marker
is using OnClusterItemClickListener
interface.
Call mClusterManager.setOnClusterItemClickListener(this);
and make your class implement OnClusterItemClickListener
Then inside the onClusterItemClick
method, you will get the ClusterItem which is the marker that was clicked,
@Override
public boolean onClusterItemClick(ClusterItem clusterItem) {
Toast.makeText(getActivity(), "Latitude " + clusterItem.getPosition().latitude, Toast.LENGTH_LONG).show();
return true;
}
来源:https://stackoverflow.com/questions/32892113/android-cluster-and-marker-clicks