MKMapView prevent annotations grouping
Asked Answered
E

1

5

I have a MKMapView with several annotations and 3 of them are very close to each other.

I used mapView.showAnnotations(mapView.annotations, animated: false) to display all annotations on the same region at launch but one of the 3 is hidden because it's too close.

I looked into Apple's Documentation but couldn't find a way to prevent this from happening, any idea how to prevent annotations grouping?

(I've never seen this before, maybe it's a iOS 11 feature)

Elroyels answered 12/9, 2017 at 15:56 Comment(2)
your annotations by default don't have grouping behavior, but if are too close then one overlaps the otherMol
@ReinierMelian Yup I saw that. Is there a way to show every annotations even if they overlap each other like on Google Maps?Elroyels
C
7

What you are seeing ist not clustering (you had to write code to get a cluster and you would usually see a cluster)

My experiments appear to suggest...

  1. MKAnnotationViews are rendered from top to bottom of the map. (It doesn't matter where north is).
  2. If MapKit decides to draw overlapping MKAnnotationViews, then the MKAnnotationView nearer to the bottom is drawn on top (because it's drawn later)
  3. Not only MKAnnotationViews, also titles rendered below MKMArkerAnnotationViews need space. The rendering of those titles is influenced by markerView.titleVisibility. If markerView.titleVisibility is set to .visible (instead of the default .adaptive), then this title is stronger than a MarkerAnnotationView that is rendered later, even if the later MarkerAnnotationView has a displayPriority = .required. The MarkerAnnotationView nearer to the bottom is not rendered.
  4. This even happens if the MarkerAnnotationView nearer to the top has a low displayPriority. So a MarkerAnnotationView with low displayPriority and .titleVisibility = .visible can make a MarkerAnnotationView nearer to the bottom with displayPriority = .required disappear.

So what should you do:

  1. make sure annotationView.displayPriority = .required for all relevant annotations. (This is necessary but not sufficient)
  2. Do not set annotationView.titleVisibility = .visible or the title will make annotationViews disappear that are rendered nearer the bottom. Instead set annotationView.titleVisibility = .adaptive

I don't think you can control the visibility completely. But the problem with annotationView.titleVisibility = .visible was surprising for me and you should avoid it because it will hide annotations.

Clusters behave like (and are) annotations that behave exactly as described above, if clusters are used.

Edit, a few years later:

Since iOS 14, MKAnnotationView has two new variables:

Set a higher value to make sure the more important MKAnnotationView stays visible. This works nicely.

For Annotations with the same zPriority, the text above is still valid.

Please be aware that Apple decided to make zPriority a property of MKAnnotationView and not of MKAnnotation.

This means as soon as MapKit decides to hide your favourite MKAnnotationView, you can't set the zPriority variable any more since the view doesn't exit any more. I solve this by setting zPriority immediately after creation of the MKAnnotationView. This last paragraph is not in Apples documentation.

Cantwell answered 12/8, 2019 at 8:19 Comment(1)
I was skeptical but the combination of .displayPriority = .required and .titleVisibility = .adaptive solved this problem for me, too.Courbevoie

© 2022 - 2024 — McMap. All rights reserved.