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.
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());
According to ClusterManager
documentation, we can use mClusterManager.cluster();
This method Force a re-cluster. You may want to call this after adding new item(s).