Clustering stop working after removing all annotations

Hi!


I noticed a strange behavior on MapKit when using the iOS 11 clustering feature.


If you add some annotations on a map (with same clusteringIdentifier and same displayPriority) MapKit will correctly merge together annotations that are close.

The problem is that if you remove all the annotations and then you add them back the map will no more merge the annotation together. It’s like the clustering feature simply stop working.


I don’t really know if it is a bug or if I miss something.

Someone else have noticed this?


Alan

Hey!


I have exactly the same issue. Did you find a way to fix it?

Hi gasho!


I found a temporary fix for the problem that seems to work in my particular situation.


After removing all the annotations I change the cluster identifier of the annotations before adding them back. If the cluster identifier change then the clustering seems to start working again.


For example if the cluster identifier of the annotations is "place", after removing the annotations you can try to set the cluster identifier to "place-2". When you add them back on the map they should cluster again.


Hope this bug will be fixed soon.


Alan

I know this is an old post, but I found a solution and thought I would post my solution for posterity. I had the same issue and found a solution better than (re)setting a completely different clusteringIdentifier as suggested. Like me, you were probably setting it in the init of your annotation view which never gets called after initializing. I moved the setting of the clusteringIdentifier to the mapView(viewFor) delegate and it works perfectly now.


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
   let view = self.map.dequeueReusableAnnotationView(withIdentifier: NSStringFromClass(SADevice.self), for: annotation)

   view.clusteringIdentifier = "cluster"

   return view
}

THANK YOU for the follow up. This helped me.

@StephaniesHusband, this is a real fix / hack. It works for me too. THANKS!

Few years later, still an issue, but just wanted to say that the proposed fix works. I was having problems when panning the map view, the clustering would stop working. The recommendation solves this.

Still haven't found a fix for the number increasing in the cluster when toggling dark/light mode... but that's a dev issue really since users can't do that with a keyboard command like we can.

Clustering stop working after removing all annotations
 
 
Q