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());
You should be able to do:
val didUpdate = clusterManager?.updateItem(MyClusterItem(data))
clusterManager?.cluster()
The key here to ensure that the clusterManager does actually update the item is to override equals and hashcode in your class that extends ClusterItem, like:
fun getId(): String {
return data.id
}
override fun equals(other: Any?): Boolean {
return other is MyClusterItem && other.getId() == getId()
}
override fun hashCode(): Int {
return getId().hashCode()
}
I was concentrating the whole time on ClusterManager, so I forgot about GoogleMap, and specifically the method clear()
. I was able to solve the problem with this code:
mGoogleMap.clear();
mClusterManager.clearItems(); // calling just in case (may not be needed)
mClusterManager.addItems(LocationGetter.getReports());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With