1

I have a mapView which I place Annotations on, however when I zoom out they disappear. I've looked at other tutorials that show that you have to put display priority as .required, but that still doesn't work for me.

Here's my code

extension MapVC: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        guard let _annotation = annotation as? MyAnnotation else {return nil}
        let identifier = "Annotation"
        var view: MKMarkerAnnotationView

        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView {
            dequeuedView.annotation = annotation
            dequeuedView.titleVisibility = .visible
            dequeuedView.displayPriority = .required
            view = dequeuedView
        }
        else
        {
           view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            view.displayPriority = .required
            view.titleVisibility = .visible
        }

        return nil
     }
  }
Tyler Rutt
  • 567
  • 2
  • 8
  • 24
  • 1
    I found this post: https://stackoverflow.com/questions/49020023/mapkit-annotations-disappearing . Maybe it can help you. – NicolasElPapu Oct 13 '19 at 18:38
  • Yea tried that, however Leszek Szary version makes the users location an annotation instead of a blue dot. Is there a way to make it a blue dot instead of an annotation? – Tyler Rutt Oct 13 '19 at 19:33
  • 1
    Sorry, actually works with a little finagling in my navigation controller. Thanks! – Tyler Rutt Oct 13 '19 at 19:37
  • 3
    @TylerRutt please make it very obvious in the question that the problem is solved (or consider deleting the question). It costs visitors time thinking about the question. – Gerd Castan Oct 27 '19 at 18:27

1 Answers1

0

If you are returning nil as view for annotation then fact that they are showing at all is incredible. As it was mentioned before this answer solves most of the problems (remarks from this answer are also valid).

If you are creating view for annotation then it should be returned at the end of this method. And to avoid replacing blue dot as annotation of current position (BTW this behaviour seems to be a bug) add this at the beginning of your delegate method:

guard annotation !== mapView.userLocation else {
            return nil
        }

Good luck!

Adam Tucholski
  • 1,067
  • 11
  • 27