2017-08-05 29 views
0

我通過CoreLocation & CLLocation得到當前位置。在MapKit上選擇一個位置並獲取經度和緯度

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

}

這是我迄今爲止,但它不工作...

回答

0

首先,你需要添加UILongPressGestureRecognizerMKMapView,而不是你的ViewController.View

用這個替換viewDidLoad方法

override func viewDidLoad() { 
    super.viewDidLoad() 
    mapKitC.delegate = self 
    let gestureZ = UILongPressGestureRecognizer(target: self, action: #selector(self.revealRegionDetailsWithLongPressOnMap(sender:))) 
    mapKitC.addGestureRecognizer(gestureZ) 
} 

之後,你的方法應該按預期

工作,希望這有助於