Search code examples
iosswiftxcodemapkitannotation

MapKit annotation how to add detail disclosure info button?


I am putting annotation on a map and after clicking the pin, there should be a detail disclosure info buttonon the right side, so I can add more code after tapping the button. But when I run the project, clicking on the pin, there is no info buttonshows up. Can anyone provide code to add disclosure info button?

I am expecting an info button on the right: enter image description here

My Code:

extension ViewController: MKMapView{


func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {



  }

Solution

  • Create a MKAnnotationView and add a button to it. So:

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation is MKUserLocation { return nil }
    
            if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "") {
                annotationView.annotation = annotation
                return annotationView
            } else {
                let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:"")
                annotationView.isEnabled = true
                annotationView.canShowCallout = true
    
                let btn = UIButton(type: .detailDisclosure)
                annotationView.rightCalloutAccessoryView = btn
                return annotationView
            }
        }