How to navigate to another page with map annotations in swift

Viewed 421

i am trying to naviagate to another page which is my details page for map markers that i have in my map controller view. so for instance if a user clicks on any marker it would take him to that markers details specifically

i created a subclass and initialized the data i need for each marker to be unique.

but now i am facing a problem with how to navigate to the next page since this annotation is not a variable.

here is my code:

my subclass:

class MyAnnotation: MKPointAnnotation{
    var shopPID: String!

    init(shopPID: String) {
         self.shopPID = shopPID
    }
    
}

adding the markers:

  db.collection("Shops").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
            
                    let lat = document["latitude"] as? String
                    let long = document["longitude"] as? String
                    let myFloat = (lat! as NSString).doubleValue
                    let myFloat2 = (long! as NSString).doubleValue
                    let annotation = MyAnnotation(shopPID: document["shopPID"] as! String)
                    annotation.coordinate = CLLocationCoordinate2D(latitude: myFloat, longitude: myFloat2)
                    annotation.title = document["name"] as? String
                    annotation.subtitle = "Click to view shop details"
                    annotation.shopPID = document["shopPID"] as? String
                    self.mapView.addAnnotation(annotation)
                    
                }
            }
        }

performing the click events and the segue:

   func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
       print(#function)
       
       if control == view.rightCalloutAccessoryView {
            performSegue(withIdentifier: "mapToDetails", sender: nil)
       }
   }

here is where i have a problem: i need to give a value to the newProjectVC that is stored in the annotations. how would i do that?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 

   if (segue.identifier == "mapToDetails") {
    
        let navigationController = segue.destination as! UINavigationController
        let newProjectVC = navigationController.topViewController as! detailsSectionViewController
        newProjectVC.getKey = //variable with each shopPID for each pin should be here
    }
    }

any help? thank you

This circle has my identifier "mapsToDetails"

1 Answers

You should use this MKMapViewDelegate method to set a custom action when your annotation is tapped.

First subclass MKAnnotation:

class MyAnnotation: NSObject, MKAnnotation {
    var shopPID: String
    var coordinate: CLLocationCoordinate2D
    let title: String?
    let subtitle: String?

    init(shopPID: String, coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
         self.shopPID = shopPID
         self.coordinate = coordinate
         self.title = title
         self.subtitle = subtitle
    }   
}

Then, on your MKMapViewDelegate use:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    
    guard let annotation = view.annotation as? MyAnnotation else { return }
    
    let uiStoryboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
    guard let vc = myStoryboard.instantiateViewController(withIdentifier: "MyViewController") as? MyViewController else { return }
    vc.shopPID = annotation.shopPID
    present(vc, animated: true)
    
}

This way you can identify the tapped annotation and pass its values to your desired VC.

Related