在地图上添加注释并打开视图

时间:2018-07-15 21:08:19

标签: swift xcode mapkit

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

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

    }

}

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

2 个答案:

答案 0 :(得分:0)

您可以创建一个具有透明背景的VC,并为其指定ID,并将此函数的手势结尾命名为

func showModal() {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "popUpId") as! popUpViewController 
    vc.providesPresentationContextTransitionStyle = true
    vc.definesPresentationContext = true 
    vc.modalPresentationStyle = .overCurrentContext
    self.present(vc, animated: false, completion: nil)
}

答案 1 :(得分:0)

您可以使用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)
}