我已经搜索了所有互联网,但没有找到解决我的(看来是)简单问题的任何解决方案。我有一张地图,用户可以在其中点击时在地图上添加注释。这是我的设置:

// Tap to set location
@IBAction func mapPoint(_ sender: UITapGestureRecognizer) {

    // Retrieve coordinates
    let location = sender.location(in: self.mapView)
    let locCoord = self.mapView.convert(location, toCoordinateFrom: self.mapView)

    // Create pin
    let annotation = MKPointAnnotation()
    annotation.coordinate = locCoord
    annotation.title = "Meeting Point"

    self.mapView.addAnnotation(annotation)

    func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {

        // Open view here
        let storyBoard: UIStoryboard = UIStoryboard(name: "Map", bundle: nil)
        let secondVC = storyBoard.instantiateViewController(withIdentifier: "secondVC") as! MapControllerMeet
        self.present(secondVC, animated: true, completion: nil)

    }

}


当用户添加注释时,我希望在地图上以模态方式打开一个新视图,换句话说,我想为我的手势识别器分配一个动作+一个按钮。这可能吗?有任何想法吗?

最佳答案

您可以使用mapView:didAddAnnotationViews:。添加注解视图后,将调用此方法。

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    // Open view here

    // You can also get added annotation
    let annotationView = views.first
    print(annotationView?.annotation?.title)
}

关于swift - 向 map 添加注释并打开 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51352230/

10-10 20:39