Google Maps Utility: how to get all markers from ClusterManager<?>?

Leonid Semyonov picture Leonid Semyonov · Mar 17, 2014 · Viewed 9.5k times · Source

Sorry for my English

I tried the ClusterManager<?>.getMarkerCollection().getMarkers() method but it returns empty collection.

I use in my app Google Maps Utility Library. Every time after a screen rotation I create AsynkTask and in background thread read data from DB and add items to ClusterManager:

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
    SomeData row = readSomeDataRow(cursor);
    clusterManager.addItem(new ClusterItemImpl(row));
    cursor.moveToNext();
}

When the AsyncTask finished its work (i.e. in main thread) I tried to get all markers from the ClusterManager:

clusterManager.cluster();
// cluster manager returns empty collection  \|/
markers = clusterManager.getMarkerCollection().getMarkers(); 

but the ClusterManager returns empty collection.

May be at the moment when I call getMarkers() the ClusterManager yet doesn't place markers on map and will do it a bit later (may be in background thread). If so, then how can I catch that moment?

Answer

Ethan picture Ethan · Jan 22, 2016

I will give you a nice workaround. First, I will give some background. Then, I will tell you the very simple method for modifying your code.

BACKGROUND: Let's first look at the implementation of ClusterManager.addItem from the library code:

public void addItem(T myItem) {
    this.mAlgorithmLock.writeLock().lock();

    try {
        this.mAlgorithm.addItem(myItem);
    } finally {
        this.mAlgorithmLock.writeLock().unlock();
    }

}

As you can see, when you call clusterManager.addItem, the ClusterManager then calls this.mAlgorithm.addItem. mAlgorithm is where your item is stored. Let's now look at the default constructor of ClusterManager:

public ClusterManager(Context context, GoogleMap map, MarkerManager markerManager) {
    ...
    this.mAlgorithm = new PreCachingAlgorithmDecorator(new  NonHierarchicalDistanceBasedAlgorithm());
    ...        
}

mAlgorithm is instantiated as a PreCachingAlgorithmDecorator containing a NonHierarchicalDistanceBasedAlgorithm. Unfortunately, since mAlgorithm is declared private, we don't have access to the items which are being added to the algorithm. However, there is happily an easy workaround! We simply instantiate mAlgorithm using ClusterManager.setAlgorithm. This allows us access to the algorithm class.

WORKAROUND: Here is your code with the workaround inserted.

  1. Put this declaration with your class variables:

    private Algorithm<Post> clusterManagerAlgorithm;
    
  2. In the place where you instantiate your ClusterManager, put this immediately afterwards:

     // Instantiate the cluster manager algorithm as is done in the ClusterManager
     clusterManagerAlgorithm = new NonHierarchicalDistanceBasedAlgorithm();
    
     // Set this local algorithm in clusterManager
     clusterManager.setAlgorithm(clusterManagerAlgorithm);
    
  3. You can leave your code for inserting items into the cluster exactly the same.

  4. When you want to get access to the items inserted, you simply use the algorithm as opposed to the ClusterManager:

    Collection<ClusterItemImpl> items = clusterManagerAlgorithm.getItems();
    

This returns the items instead of the Marker objects, but I believe it is what you need.