Add ID for each annotation in MapKit

I need to add an Id for each annotation in the map to open a new view controller by clicking the info button in the annotation, my problem is preparing and executing the segue, I created it, but I don't know how to pass the id to the segment.

I created an annotation subclass to store the ID that comes from JSON

Here are the parts of my code:

Getting all data from JSON:

for location in self.locations {

  let annotation:MyAnnotation = MyAnnotation()
  annotation.title = location["truck_name"] as? String
  annotation.customTruckId = location["truck_id"] as? String
  annotation.coordinate = CLLocationCoordinate2D(latitude: (location["coor_x"] as! NSString).doubleValue, longitude: (location["coor_y"] as! NSString).doubleValue)
  self.AllMapView.addAnnotation(annotation)

  }

      

Subclass I created:

class MyAnnotation : MKPointAnnotation {
    var customTruckId : String?
}

      

The problem is in the section here:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "ShowTruckFromMap") {

            let des_VC = segue.destination as! TruckDetailsVC

            des_VC.truck_id = "How to get Id here !"
        }
    }


    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if control == view.rightCalloutAccessoryView {

            performSegue(withIdentifier: "ShowTruckFromMap", sender: self)
        }
    }

      

thank

+3


source to share


2 answers


You can show your UIViewController without using Segue. First you have to put an ID in your UIViewController in: identity inspector -> Identity -> storyboard ID = TruckDetailsVCIdentifier



func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if control == view.rightCalloutAccessoryView {
           let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
           let desController = mainStoryboard.instantiateViewController(withIdentifier: "TruckDetailsVCIdentifier") as! TruckDetailsVC
           let TruckAnnotation = view.annotation as? MyAnnotation
           desController.truck_id = (TruckAnnotation?.customTruckId!)!
           show(desController, sender: self)

        }
    }

      

+2


source


I found the answer:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if control == view.rightCalloutAccessoryView {
            let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let desController = mainStoryboard.instantiateViewController(withIdentifier: "TruckDetailsVC") as! TruckDetailsVC

            let TruckAnnotation = view.annotation as? MyAnnotation

            desController.truck_id = (TruckAnnotation?.customTruckId!)!

            show(desController, sender: self)

        }
    }

      



thanks for all

+1


source







All Articles