Update single item GoolgeMap Cluster

2020-04-05 08:15发布

问题:

I am using this libray to cluster GoogleMap in Android. My question is how can I update the single item I have gone through google from yesterday and no answers are there that explains updating single item. I am using websocket in my project so I need to update the data of item that were received from websocket. Look my implementation below.

My concept is doing mClusterManager.remove(item) mClusterManager.add(item) + mClusterManager.cluster() whenever I receive data from websocket.

and hasmap to identify the object on loop while adding to cluseter like : hashmap.put(_id,mClusterItem[i]);

Now, Whenever on websocket data is received I do,

    onDataReceive(String _id,String name, double latlng, ....){
    mClusterManager.remove(hashmap.get(_id));

   appClusterItem[0] = new AppClusterItem(.....);
    mClusterManager.add(appClusterItem[0])  // Here how can I add item 
    mClusterManager.cluster();
    }

However the above code works first when first data receives, then from second time it will just keep adding the marker and fails to remove that means mClusterManager.remove(hasmap.get(_id)) is not found. And appClusterItem[0] is because I cannot use hashmap.get(_id); on above case bacause it give error variable expected. Anyway to remove the same object and add object on that place??

回答1:

I also tried to remove marker from cluster via mClusterManager.remove and have some problem with it. So in my case, when I received data changes I make this: I remove item that i need to remove from my list, clear all markers on cluster with mClusterManager.clearItems(); and put fresh data to cluster.



回答2:

ClusterManager is having removeItem() defined as below

public void removeItem(T item) {
        mAlgorithmLock.writeLock().lock();
        try {
            mAlgorithm.removeItem(item);
        } finally {
            mAlgorithmLock.writeLock().unlock();
        }
}

You need to pass custom Item object that might be extended from ClusterItem. Check the method documentation from library class defined here.



回答3:

Have you tried to recluster when removing and recluster again when adding? I think I solve mine with that. Your code would be:

 onDataReceive(String _id,String name, double latlng, ....){
mClusterManager.remove(hashmap.get(_id));
mClusterManager.cluster();                   //add this line
appClusterItem[0] = new AppClusterItem(.....);
mClusterManager.add(appClusterItem[0])  // Here how can I add item 
mClusterManager.cluster();                   //leave this one here
hashmap.remove(_id);
hashmap.put(_id,mClusterItem[0]); //also don't forguet to update your hashmap
}

let me know if it worked for you!