mapkit 中的 onclick 事件弹出 xcode 8
onclick event popup in mapkit xcode 8
我在 xcode mapkit 中使用 Open Street Map 完成了一个地图项目,我已经在其中添加了地图并创建了折线、多边形、带有弹出窗口的标记。
我还有两部分要做一是给地图添加缩放按钮
- 第二个是我需要添加一个 onclick 事件,当我触摸地图中的某处时,我需要在地图上显示一个
显示 "hey u touch this lat,long". 的弹出事件
我使用了多种方法进行研究,但我无法实现我的目标
首先,您需要在 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)
}
我在 xcode mapkit 中使用 Open Street Map 完成了一个地图项目,我已经在其中添加了地图并创建了折线、多边形、带有弹出窗口的标记。 我还有两部分要做一是给地图添加缩放按钮
- 第二个是我需要添加一个 onclick 事件,当我触摸地图中的某处时,我需要在地图上显示一个
显示 "hey u touch this lat,long". 的弹出事件
我使用了多种方法进行研究,但我无法实现我的目标
首先,您需要在 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)
}