mapkit 中的 onclick 事件弹出 xcode 8

onclick event popup in mapkit xcode 8

我在 xcode mapkit 中使用 Open Street Map 完成了一个地图项目,我已经在其中添加了地图并创建了折线、多边形、带有弹出窗口的标记。 我还有两部分要做一是给地图添加缩放按钮

我使用了多种方法进行研究,但我无法实现我的目标

首先,您需要在 viewDidLoad 方法

中向 MKMapView 添加一个 UITapGestureRecognizer

像这样

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.showPopup(tapGesture:)))
    self.mapView.addGestureRecognizer(tapGestureRecognizer)

之后你需要实现 Tap 选择器方法,你会发现的主要问题是根据触摸屏坐标获取真实位置,你可以使用 MKMapView convert 方法

像这样

let coordinate = self.mapView.convert(tapGesture.location(in: self.mapView), toCoordinateFrom: self.mapView) //here we convert touch location from Screen location to map coordinates

您的完整代码用于显示带有触摸坐标的警报弹出窗口

func showPopup(tapGesture:UITapGestureRecognizer)
{
    let coordinate = self.mapView.convert(tapGesture.location(in: self.mapView), toCoordinateFrom: self.mapView)
    let alertViewController = UIAlertController(title: "TOUCH IN MAP", message: "coordinate is lat: \(coordinate.latitude) long: \(coordinate.longitude)", preferredStyle: .alert)
    let action = UIAlertAction(title: "Accept", style: .destructive, handler: nil)
    alertViewController.addAction(action)
    self.present(alertViewController, animated: true, completion: nil)
}