Android-maps-utils: Grouping Cluster

Created on 6 Aug 2014  路  18Comments  路  Source: googlemaps/android-maps-utils

I am using the library for one of my project, but I was wondering if it is possible to cluster my marker by group. For example, I want to cluster only markers that are "Friends" and cluster the others who are only "Coworker" and etc... (Maybe not the best example, but I hope that you understand)

My idea was to use multiple ClusterManager but I didn't tried it and don't really know if it is the best solution or even a good solution.

Thanks for the reply.

Most helpful comment

Hi ! i'm facing the same issue, i can't figure out how to manage multiple cluster managers to make cluster groups as we can do in "android-maps-extension".
I'm not sure i understand your solution @jaayesh, which manager's listeners are you binding with the map ? Like @qthurier said if you declare 3 Cluster Manager, you need to bind setOnCameraChangeListener and setOnMarkerClickListener to a manager not 3, so how do you perform your trick ? Maybe a little example would help ? :)
Thanks !

All 18 comments

What you could do is using your custom cluster renderer and mark the cluster icons with an identifier.

If onbeforecluster (does not have the right name present) gets called you could group the items manually by creating different buckets for each type of cluster.

Its not easy but I think it would work.

Yes, multiple ClusterManagers is probably the best way to do that. It should work fine.

Ping this thread if it doesn't work.

I have around 10 different categories of markers, lets say Pizza, Burger, Pasta, Cake, Pepsi and so on. Now if I have to put markers of type pizza under a cluster Pizza, markers of type cake under a cluster Cake like wise. In this scenario creating 'n' number of ClusterManager will not be a feasible solution. I would like to know a better way if there is any for this problem.

Thanks in advance!

I have a similar case. Having different cluster types where each contains markers of a certain kind. So like a cluster clusters nearby markers which are of the same type, as @jaayesh described by example.
I think this is a common use case which should be covered by the library. @broady Could we open this issue again?

Did anyone get a working solution yet?

@broady How do you set different related GoolgeMap listeners for two different ClusterManagers simultaneously?

@broady I tried it for multiple cluster managers and the listeners are applied only to the latest cluster manager that the listeners are being set to. Could you handle this in the library or atleast suggest a way to handle it ?

Hello,
I have the same problem for using multiple cluster managers. Does any one found a solution for that?
Thank you in advance

Hello,
I'm facing the same problem. I can't figure out how to handle multiple cluster managers. My listeners are applied only to the latest cluster manager. Does anyone have a solution?
Many thanks.

Hi qthurier,

Well, I did not find any other solution but to use 'n' cluster manager with associated 'n' cluster renderer. This solution worked for me. All you have to do is to ensure you set data to respective appropriate cluster manager.
As far as listeners are concerned you can always get respective manager with a bit dirty way, need to use onMarkerClick listener only. Example: At the time of adding Marker to manager ensure you set either title or snippet as "Pizza" and add it to respective manager i.e. PizzaManager. Then upon click of Marker, onMarkerclick will be invoked and there you can check for title/snippet and can retrieve respective manager or renderer.

I know this not at all a good way but thats how it works with this library.

Hope this helps!

Hi ! i'm facing the same issue, i can't figure out how to manage multiple cluster managers to make cluster groups as we can do in "android-maps-extension".
I'm not sure i understand your solution @jaayesh, which manager's listeners are you binding with the map ? Like @qthurier said if you declare 3 Cluster Manager, you need to bind setOnCameraChangeListener and setOnMarkerClickListener to a manager not 3, so how do you perform your trick ? Maybe a little example would help ? :)
Thanks !

Any solutions?

I'm also looking for this

For this you can implement OnCameraMoveListener and on moving or zooming of map you can force to clustermanger to make cluster in onCameraMove method. Like this,
if (mClusterManager != null)
mClusterManager.cluster();

@jaayesh how to add multi cluster manager in a map? I try with
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
mClusterManager1.onCameraChange(cameraPosition);
mClusterManager2.onCameraChange(cameraPosition);
}
});
but it can't. Can you share your code add multiple cluster manager?

@thangkho What exactly do you mean by, "how to add multi cluster manager in a map?", if you want to update respective cluster manager with respect to map's camera change event, you need to call manager.cluster()

setOnCameraChangeListener is now deprecated. You can use setOnCameraIdleListener.

Calll cluster method on each clusterManager:

mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
             @Override
             public void onCameraIdle() {
                 for (ClusterManager<MarkerClusterItem> cm:clusterManagerList)
                 {
                     cm.cluster();
                 }
             }
         });
    googleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
        @Override
        public void onCameraIdle() {
            clusterManager.cluster();
            clusterManager2.cluster();
        }
    });

When using multiple ClusterManager, the listeners are still overwritten by the latest ClusterManager.

private lateinit var clusterManagerA: ClusterManager<AItem>
private lateinit var clusterManagerB: ClusterManager<BItem>

override fun onMapReady(googleMap: GoogleMap) {
    clusterManagerA = ClusterManager(this, googleMap)
    clusterManagerA.setOnClusterItemInfoWindowClickListener {
        Log.d(TAG, "clusterManagerA onClusterItemInfoWindowClick")
    }
    clusterManagerA.addItem(AItem())

    clusterManagerB = ClusterManager(this, googleMap)
    clusterManagerB.setOnClusterItemInfoWindowClickListener {
        Log.d(TAG, "clusterManagerB onClusterItemInfoWindowClick")
    }
    clusterManagerB.addItem(BItem())
}

Now when I click on the InfoWindow of:

  • AItem: Nothing happens
  • BItem: It shows the log message

My current workaround using one ClusterManager:

private lateinit var clusterManager: ClusterManager<ClusterItem>

override fun onMapReady(googleMap: GoogleMap) {
    clusterManager = ClusterManager(this, googleMap)
    clusterManager.setOnClusterItemInfoWindowClickListener {
        when (it) {
            is AItem -> Log.d(TAG, "onClusterItemInfoWindowClick AItem")
            is BItem -> Log.d(TAG, "onClusterItemInfoWindowClick BItem")
        }
    }
    clusterManager.addItem(AItem())
    clusterManager.addItem(BItem())
}

Is there a solution/workaround for grouping clusters with listeners?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CodeHore picture CodeHore  路  4Comments

dadino picture dadino  路  7Comments

YuriPopiv picture YuriPopiv  路  5Comments

edumucelli picture edumucelli  路  5Comments

WeiLinJoe picture WeiLinJoe  路  4Comments