swiftmapkitmkpinannotationview

Swift map pin callout and param


I made a map on my iOS Application using MapKit.

I added my pins to my view with a callout button which present de detail button inside the pin popup.

At this time, everything is good, when I tap on the detail button, I can print some text, present a new view controller but my problem is that I can't figure out how I can know which pin I've tapped.

I can solve it by using the title but it's not the best way for me, I prefer use my item id instead of a string.

If anyone knows how I can add an "id" property ton my pin or use the subtitle property (without show it on the popup bubble), I'll be grateful :)

Thank you for your help.

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

    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customAnnotation")
    annotationView.image = UIImage(named: "pin")
    annotationView.canShowCallout = true
    annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

    return annotationView
}


func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl){

    print("OK, item tapped.")
}

Solution

  • You can subclass MKPointAnnotation to add the ID property

    class CustomPointAnnotation: MKPointAnnotation {
        let id: Int
    
        init(id: Int) {
            self.id = id
        }
    }
    

    Usage

    let annotation = CustomPointAnnotation(id: INTEGER)
    annotation.coordinate = CLLocationCoordinate2D(latitude: DOUBLE, longitude: DOUBLE)
    mapView.addAnnotation(annotation)
    
    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if let annotation = view.annotation as? CustomPointAnnotation {
            print("Annotation \(annotation.id)")
        }
    }
    

    You can also create your own annotation class by extending the base MKAnnotation protocol, like so:

    class CustomAnnotation: NSObject, MKAnnotation {
        let id: Int
        let coordinate: CLLocationCoordinate2D
    
        init(id: Int, latitude: Double, longitude: Double) {
            self.id = id
            self.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        }
    }