gpt4 book ai didi

ios - 在MapKit上选择一个位置并获取经度和纬度

转载 作者:行者123 更新时间:2023-12-01 22:19:54 24 4
gpt4 key购买 nike

我实现了通过CoreLocationCLLocation获取当前位置。

在另一个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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com