Anyone else tried out the new MKPointAnnotation clustering feature?
I have serious performance issues on the map after adding 900 (or less) annotations to mapkit on low zoom levels. (zooming, panning etc.) The annotations are getting clustered correctly on each region change event but the map is laggy and unresponsive. I have a set of "stops" (public means of transport) that is loaded on higher zoom values (zoom in). On lower zoom values (zoom out), I am using "departures" (about 3x the count of the visible "stops"). I did the clustering manually before and there was no lag at all.
The documentation and WWDC 2017 videos do not mention anything about performance or best practice for efficient clustering. In general, I only add annotations that can be visible in the current mapRect (of course, when zooming out they become more an moreā¦)
I've also tried out the MapKit demo app "Tandm" with about 900 annotations and it had similar performance issues.
I also ran the profiler and the performance issues seem to be caused by the internal MKAnnotation renderer when it updates the visible annotations. I think the main issue is, that there are simply too many visible clusters. I haven't found a way to change that behavior to fit my needs. I have tried setting the clusterIdentifier to different values and ended up setting it always to "stops" works "best". Of course I could limit the data when zooming out but I think the clustering should be able to handle that.
Here is a video: https://www.dropbox.com/s/8bubd9ib76pmz47/mapkit-lag.mov?dl=0
The function visibleAnnotations() takes about 30% of the execution time 70% are taken by MapKit when updating the annotations.
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if #available(iOS 11.0, *) {
if let visibleAnnotations = visibleAnnotations() {
let oldAnnotations = mapView.annotations
mapView.removeAnnotations(oldAnnotations)
mapView.addAnnotations(visibleAnnotations)
}
}
}