How update markers on Map when using ClusterManager

United121 picture United121 · Sep 22, 2015 · Viewed 9.5k times · Source

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());

Answer

Pravin Divraniya picture Pravin Divraniya · Sep 21, 2016

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).