我正在尝试使用touchesBegan函数和tapGesture获取地图上点的坐标,但我得到的坐标表示与地图上选定点不同的点。有什么帮助吗?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let tapGesture = UITapGestureRecognizer(target: self, action: nil)
let touchPoint = tapGesture.locationInView(self.myMap)
let location = self.myMap.convertPoint(touchPoint, toCoordinateFromView: self.myMap)
let mySpan:MKCoordinateSpan = MKCoordinateSpanMake(0.5, 0.5)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, mySpan)
self.myMap.setRegion(region, animated: true)
lblLat.text = String(location.latitude)
lblLng.text = String(location.longitude)
}
答案 0 :(得分:0)
用以下代码替换您的代码:
func tapGestureOnMap(gestureRecognizer: UITapGestureRecognizer) {
let touchLocation = gestureRecognizer.locationInView(mapView)
let locationCoordinate = mapView.convertPoint(touchLocation, toCoordinateFromView: mapView)
print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
}
在viewDidLoad
中,在地图视图中添加tapGesture
,如下所示:
let tapGestureOnMap = UITapGestureRecognizer(target: self, action: "tapGestureOnMap:")
mapView.addGestureRecognizer(tapGestureOnMap)