In my Android application, I have to delete and re-add a cluster item in my GoogleMap, that represents my current location. But when I run this code:
clusterMng.remove(myitem);
I get this Exception:
java.lang.UnsupportedOperationException: NonHierarchicalDistanceBasedAlgorithm.remove
not implemented.
Can someone explain to me what this means? Do I have to rewrite some methods of ClusterManager.java in the external library? Or can I simply change my algorithm?
By default ClusterManager uses NonHierarchicalDistanceBasedAlgorithm, that doesn't implement removing elements.
Try to use GridBasedAlgorithm instead (it supports elements remove):
Or, for better performance, wrap it with PreCachingAlgorithmDecorator, as ClusterManager does by default:
Here is how I did it:
I also implemented
equals()
andhashCode()
inQuadItem
as it is recommended in the TODO of theNonHierarchicalDistanceBasedAlgorithm
source code:Finally, I implemented
equals()
andhashCode()
in myClusterItem
's descendant class.I have found
removeItem(T item)
in the source code of ClusterManager.java Tested, it works Link to source code https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/clustering/ClusterManager.javaAs @SergePopulov said, NonHierarchicalDistanceBasedAlgorithm does not implement removing elements. For those who dont want to use GridBasedAlgoritm but still needs to remove single elements from NonHierarchicalDistanceBasedAlgorithm there is another solution.
Using this link (Source) you can find source code for the NonHierarchicalDistanceBasedAlgorithm provided by developers in github.
What I did is just save the old Cluster items, clear the clusterManager and add the old items again but do not add the one that is passed through the method.
Firstly create a separate class and paste NonHierarchicalDstanceBasedAlgorithm class code.
After that find method removeItem and replace it with this code:
After that go where your ClusterManager is created and paste code below containing your class name:
Where your MarkerItem is your class which implemented ClusterItem. And it should now work.
Don't forget to recluster your ClusterManager after you remove the item by running:
After updating
android-maps-utils
to0.5
clusterManager.remove(item);
will never throw anUnsupportedOperationException("NonHierarchicalDistanceBasedAlgorithm.remove not implemented")
Refer this thread