我实现了通过CoreLocation
和CLLocation
获取当前位置。
在另一个UIViewController中,我想在MapKit
上选择一个位置,并从MapKit
获取纬度和经度。我进行了很多搜索,但是发现了一些使用Objective-C的 class 。
还是Swift有什么 class ?
class CustomLocationVC: UIViewController, MKMapViewDelegate, UIGestureRecognizerDelegate {
@IBOutlet weak var mapKitC: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapKitC.delegate = self
let gestureZ = UILongPressGestureRecognizer(target: self, action: #selector(self.revealRegionDetailsWithLongPressOnMap(sender:)))
view.addGestureRecognizer(gestureZ)
}
@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
if sender.state != UIGestureRecognizerState.began { return }
let touchLocation = sender.location(in: mapKitC)
let locationCoordinate = mapKitC.convert(touchLocation, toCoordinateFrom: mapKitC)
print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
}
}
这是我到目前为止所拥有的,但是不起作用...
最佳答案
首先,您需要将UILongPressGestureRecognizer
而不是MKMapView
添加到ViewController.View
用这个替换您的viewDidLoad
方法
override func viewDidLoad() {
super.viewDidLoad()
mapKitC.delegate = self
let gestureZ = UILongPressGestureRecognizer(target: self, action: #selector(self.revealRegionDetailsWithLongPressOnMap(sender:)))
mapKitC.addGestureRecognizer(gestureZ)
}
之后,您的方法应按预期工作
希望这可以帮助
关于ios - 在MapKit上选择一个位置并获取经度和纬度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45521346/