0

In 2017 MKMarkerAnnotationView was announced to replace MKPinAnnotationView. As described by this WWDC video at 12:21, MKMarkerAnnotationView has three states:

  1. Normal
  2. Selected
  3. Selected with Callout

How do you programmatically set the "Selected with Callout" state so that it displays as it does in the WWDC video? This seems like it should be a super straight forward thing to do, but I see absolutely nothing in the MapKit documentation, the only way I can get it to work reliably is this:

enter image description here

Documentation Links:

  1. MKAnnotationView
  2. MKMarkerAnnotationView
1

1 Answer 1

1

You can use canShowCallout property.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKMarkerAnnotationView
    if pinView == nil {
        pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true

        let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
        pinView?.rightCalloutAccessoryView = rightButton as? UIView

    }
    else {
        pinView?.annotation = annotation
    }
    return pinView
}

And you need to select the annotation to set the "Selected with Callout" state.

mapView.selectAnnotation(annotation, animated: true)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.