6

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??

Queendevelopers
  • 183
  • 3
  • 20

3 Answers3

2

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.

Vadim Eksler
  • 865
  • 9
  • 24
  • mCluster.clearItems(); also needs to clear googleMap.clear(); right?? I think this will not be feasible and stablilty while working with large data later on. Currently I am receving about 50 items in second i.e. 50 times clearing items from cluster and adding them all. will this be good for later?? – Queendevelopers Nov 21 '18 at 08:56
  • 1
    I not use `googleMap.clear();` just cluster clear. I don't know if it will be good for your case. look at here https://stackoverflow.com/a/22204134/7917629, it can be helpful to change algorithm or writ self algorithm. – Vadim Eksler Nov 21 '18 at 09:01
  • Another trouble I am getting while using clearItems(); and adding them as you suggested is I cannot check InfoWindows is showing or not. Whenever a data is received a InfoWindow is closed !! – Queendevelopers Nov 22 '18 at 08:56
  • only one Info Window can be open at a time, you can consider using GoogleMap.OnInfoWindowCloseListener to know when an Info Window has been closed – Vadim Eksler Nov 22 '18 at 10:51
  • You may be right, What I mean to say is I am using many markers to display and their Info Window with their values, now when users click on some specific marker the infowindow shows to them, now whenever the data is received the infowindow closes, I know I can use showInfoWindow() for a marker but how could I know, which marker was showing Info before?? – Queendevelopers Nov 25 '18 at 06:34
  • @Queendevelopers do you use custom infowindow adapter like `mMap.setInfoWindowAdapter()` ? maybe it can help to you? – Vadim Eksler Nov 28 '18 at 10:02
  • Yes.. I am using custom `InfoWindowAdapter()` like `mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(new CustomAdapterInfoWindow());` – Queendevelopers Nov 28 '18 at 11:02
  • like this - `mMap.setInfoWindowAdapter(mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(new CustomAdapterInfoWindow()))` ? – Vadim Eksler Nov 28 '18 at 11:06
  • about marker that was showing Info before, in your onClusterItemClick you get your marker, if it have some uniq id? you can save id of marker that was showing Info before, and show after data is received. – Vadim Eksler Nov 28 '18 at 11:22
  • If I keep trackerId as key then how could I check whenever the data is received? I need marker instance to check the showInfoWindow like `if(marker.isInfoWindowShown()){//code goes here}` , here marker will still be an anonymous!! – Queendevelopers Nov 28 '18 at 11:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184399/discussion-between-queendevelopers-and-vadim-eksler). – Queendevelopers Nov 28 '18 at 11:56
  • Hey.. broo I finally tried to implement your way, but as per our discussion on our chat render.getMarker(ListOfAppClusterItems.getItemByID(idOfShowingInfo)).showInfoWin‌​dow(); at line 315 I could not get the Id. What I tried was render.getMarkser(appClusterItem.getmId()).showInfoWindow(); but I could not get anything from appClusterItem. How did you do you can you please explain me a little bit? – Queendevelopers Jan 07 '19 at 06:18
1

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.

karan
  • 8,637
  • 3
  • 41
  • 78
  • Thanks for the answer!! A little long explanation with more code will be helpful anyway I will go through the link you provided and check if it help. – Queendevelopers Nov 30 '18 at 09:49
0

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!

Daniel
  • 21
  • 1
  • 6
  • Thanks for your reply. Yes I also did almost same way.. now the problem is in showingInfoWindow of previous item if infowindow is shown. How do you deal with infowindow after update? – Queendevelopers Oct 02 '19 at 07:00