Home » Android » android – add multiple ClusterManager to Google map

android – add multiple ClusterManager to Google map

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am trying to use two ClusterManager for Google Map , But I am able to add only one clustermanager and it’s item click event,

googleMap.setOnMarkerClickListener(mClusterManager);

and

mClusterManager.setOnClusterItemClickListener(new OnClusterItemClickListener<Person>() {

    @Override
    public boolean onClusterItemClick(Person item) {
        Log.v("Cluster Click", "Item Click:"+item.getName());
        return true;
    }
});

How can I add one more clustermanager to googlemap?

Thanks in advance.

How to&Answers:

you can use MarketManager like this:

MarkerManager markerManager = new MarkerManager(mMap);

mClusterManager1 = new ClusterManager<>(getContext(), mMap,markerManager);
mClusterManager2 = new ClusterManager<>(getContext(), mMap,markerManager);

mMap.setOnMarkerClickListener(markerManager);

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            mClusterManager1.onCameraChange(cameraPosition);
            mClusterManager2.onCameraChange(cameraPosition);
        }
    });


mClusterManager1.setRenderer(myClusterRenderer1 = new MyClusterRenderer(getContext(), mMap, mClusterManager1));
mClusterManager2.setRenderer(myClusterRenderer2 = new MyClusterRenderer(getContext(), mMap, mClusterManager2));

Answer:

yes, delegate the click listener through to both cluster managers.

map.setOnClickListener(new GoogleMap.OnMarkerClickListener() {
  boolean onMarkerClick(Marker m) {
    clusterManager1.onMarkerClick(m)
    clusterManager2.onMarkerClick(m)
  }
});