Daniel Patriarca
Daniel Patriarca

Reputation: 365

annotation Display Priority doesn't do what I expect, how to keep custom pin on screen

I use the code below on view did load to add a custom annotation icon for the map center that the user started at so that if they scroll away they can always see their starting point.

    if let lat = curBlip.blip_lat, let lon = curBlip.blip_lon {
        let mapCenter = CLLocationCoordinate2DMake(lat, lon)
        let mapSpan = MKCoordinateSpanMake(0.01, 0.01)
        let mapRegion = MKCoordinateRegionMake(mapCenter, mapSpan)
        self.map.setRegion(mapRegion, animated: true)

        let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = "Your Blips Location"
        annotation.subtitle = "Subtitle Placeholder"

        self.map.addAnnotation(annotation)

When the view loads I load that annotation so its always first and I set a bool named "set" to true after the first annotation to ensure that it gets the custom icon. The issue I am having is that even though I have the annotation set to display priority required the annotation disappears when I move the map away. How can I make that annotation always persist or is there a better way to set a "this is where the map started" circle that doesn't go away?

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if set {
        return nil
    } else {
        let view = MKAnnotationView(annotation: annotation, reuseIdentifier: "annotationId")
        view.image = UIImage(named: "locationArea50")
        view.canShowCallout = true
        view.displayPriority = .required
        set = true
        return view
    }

Blue circle annotation appears

Then after I scroll the map away a little bit, I suspect scrolling far enough that the system has to make them reappear the annotation disappears. I assume this has to do with how the grouping of annotations works but that blue annotation is special and I want it to always be present, which is what I thought displayPriority did.

enter image description here

Upvotes: 0

Views: 1467

Answers (1)

Daniel Smith
Daniel Smith

Reputation: 960

The default value of displayPriority is .required . So for correct overlapping you need to downgrade priority of red annotations:

redAnnotation.displayPriority = .defaultHigh

Upvotes: 0

Related Questions