问题
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
回答1:
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.
回答2:
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;
}
回答3:
Here you can see my code. The example is complete with all you need.
public class HomeFragment extends Fragment implements OnMapReadyCallback, ClusterManager.OnClusterClickListener<MarkerClusterItem> {
...
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
this.googleMap = googleMap;
this.googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
setUpClusterManager();
...
}
private void setUpClusterManager(){
// cluster
clusterManager = new ClusterManager<MarkerClusterItem>(getActivity(), this.googleMap);
clusterManager.setAnimation(false);
MarkerClusterRenderer clusterRenderer = new MarkerClusterRenderer(getActivity(), googleMap, clusterManager);
clusterManager.setRenderer(clusterRenderer);
// marker clic
clusterManager.setOnClusterClickListener(this);
}
@Override
public boolean onClusterClick(Cluster<MarkerClusterItem> cluster) {
if (cluster == null) return false;
// do your stuff here
return true;
}
}
来源:https://stackoverflow.com/questions/32892113/android-cluster-and-marker-clicks