简体   繁体   English

如何快速导航到带有地图注释的另一个页面

[英]How to navigate to another page with map annotations in swift

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.这是我遇到问题的地方:我需要为存储在注释中的 newProjectVC 赋值。 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"这个圆圈有我的标识符“mapsToDetails”

You should use this MKMapViewDelegate method to set a custom action when your annotation is tapped.当您的注释被点击时,您应该使用此 MKMapViewDelegate 方法来设置自定义操作。

First subclass MKAnnotation:第一个子类 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:然后,在您的 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)
    
}

This way you can identify the tapped annotation and pass its values to your desired VC.通过这种方式,您可以识别点击的注释并将其值传递给您想要的 VC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM