Search code examples
swiftuiuikitmapkitmapkitannotation

Hide information under map annotation


The default MapMarker hides information that is under it, for example a city name. If zoomed closed enough the marker doesn't cover the name anymore and the name is displayed. When using a custom annotation view both the city name and the annotation is displayed, it looks a lil messy. Is it possible to get the behaviour from MapMarker when using custom annotations? MapMarker: https://i.sstatic.net/OhQxj.png Custom: https://i.sstatic.net/GwO23.png

Maybe something with clustering or collisions, but seems do be annotations colliding with annotations.


Solution

  • setting the value of .displayPriority to some value seems to do the trick. Don't know which value to set though.

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let annotation = MKAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
        annotation.image = UIImage(systemName: "circle.fill")
        annotation.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        annotation.displayPriority = .defaultHigh
                
        return annotation
    }