United121 United121 - 2 months ago 32
Android Question

How update markers on Map when using ClusterManager

I am trying to update my map with newer data from server but I can't figure out how to remove old items (markers and cluster - Using ClusterManager) from map ( - seems to me that I can only add in ClusterManager.

I have BroadcastReceiver which get intent when there are new data. I was trying something like this: ( but it gives me UnsupportedOperationException on line with "...getMarkers().clear();"

private BroadcastReceiver myRefrestMapBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
mClusterManager.clearItems();
mClusterManager.getMarkerCollection().getMarkers().clear();
mClusterManager.getClusterMarkerCollection().getMarkers().clear();
mClusterManager.addItems(LocationGetter.getReports());
}
};


I added data to map only with this function.

Thank you in advance.

EDIT1

java.util.Collection<Marker> userCollection = mClusterManager.getMarkerCollection().getMarkers();
ArrayList<Marker> userList = new ArrayList<Marker>(userCollection);
// now is userList empty
for(Marker marker: userList){
marker.remove();
}

java.util.Collection<Marker> userCollection2 = mClusterManager.getClusterMarkerCollection().getMarkers();
ArrayList<Marker> userList2 = new ArrayList<Marker>(userCollection2);
// now is userList2 empty
for(Marker marker: userList2){
marker.remove();
}

mClusterManager.addItems(LocationGetter.getReports());

Answer

I was the whole time concert on ClusterManager, so I forget on GoogleMap and method clear() so I hope that I solve it with this code :

mGoogleMap.clear();
mClusterManager.clearItems(); // calling for sure - maybe it doenst need to be here
mClusterManager.addItems(LocationGetter.getReports());
Comments