【问题标题】:Choosing a location on MapKit and get latitude and longitude在 MapKit 上选择一个位置并获取纬度和经度
【发布时间】:2017-08-06 01:38:01
【问题描述】:

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

在另一个 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)")
}

}

这是我目前所拥有的,但它不起作用......

【问题讨论】:

    标签: ios swift mkmapview mapkit


    【解决方案1】:

    首先,您需要将您的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)
    }
    

    之后,您的方法应该按预期工作

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 2011-01-30
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多