Has anyone else tried out MKPointAnnotation's new clustering feature?
I have serious map performance issues after adding 900 (or less) annotations to mapkit at low zoom levels. (zooming, panning, etc.) Annotations are ordered correctly on each event of a change in a region, but the map is inactive and does not respond. I have a set of “stops” (public vehicles) that load at higher scaling values (increase). At lower scaling values (decrease), I use “deviations” (about 3x account of visible “stops”). I used to cluster manually and there was no lag.
WWDC 2017 docs and videos don't say anything about performance or best practices for efficient clustering. In general, I add only annotations, which can be visible in the current mapRect (of course, when scaling, they become more ...)
I also tested the MapKit "Tandm" demo application with approximately 900 annotations and had similar performance issues.
I also ran the profiler, and performance issues seem to be caused by the MKAnnotation internal renderer when it updates the visible annotations. I think the main problem is that there are simply too many visible clusters. I have not found a way to change this behavior according to my needs. I tried to set different values for the clusterIdentifier parameter and eventually found that it always “stops”, works “better”. Of course, I could limit the data when scaling, but I think that clustering should be able to handle this.
Here is the video: https://www.dropbox.com/s/8bubd9ib76pmz47/mapkit-lag.mov?dl=0
The visibleAnnotations () function takes about 30% of the execution time, 70% is accepted by MapKit when updating 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)
}
}
}