swift 如何使用地图注释导航到另一个页面

How to navigate to another page with map annotations in swift

我正在尝试导航到另一个页面,这是我的地图控制器视图中的地图标记的详细信息页面。因此,例如,如果用户点击任何标记,他会具体了解该标记的详细信息

我创建了一个子类并初始化了我需要的数据,每个标记都是唯一的。

但现在我遇到了如何导航到下一页的问题,因为此注释不是变量。

这是我的代码:

我的子类:

class MyAnnotation: MKPointAnnotation{
    var shopPID: String!

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

添加标记:

  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)
                    
                }
            }
        }

执行点击事件和 segue:

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

这是我遇到问题的地方:我需要为存储在注释中的 newProjectVC 赋值。 我该怎么做?

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
    }
    }

有什么帮助吗?谢谢你

这个圈子有我的标识符“mapsToDetails”

您应该使用此 MKMapViewDelegate 方法来设置点击注释时的自定义操作。

第一个子类 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
    }   
}

然后,在您的 MKMapViewDelegate 上使用:

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)
    
}

这样您就可以识别点击的注释并将其值传递给您想要的 VC。