I implemented to get the current location by CoreLocation
& CLLocation
.
In another UIViewController, I want to choose a location on MapKit
and get latitude and longitude from MapKit
. I searched a lot but I found a few courses using Objective-C.
Or is there any course in 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)")
}
}
This is what I have so far but it doesn't work...
答案 0 :(得分:0)
首先,您需要将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)
}
之后,您的方法应按预期工作
希望这有帮助