我实现了通过CoreLocation
&获取当前位置CLLocation
。
在另一个 UIViewController 中,我想选择一个位置MapKit
并从MapKit
. 我搜索了很多,但我找到了一些使用 Objective-C 的课程。
或者有 Swift 的课程吗?
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)
}
之后你的方法应该按预期工作
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句