谁能解释,为什么委托方法'CLLocationManager(didUpdateLocations)'被调用了4次,尽管我立即停止了更新?我只需要一次当前位置即可:)
import UIKit
import MapKit
class ContactView: UIViewController, CLLocationManagerDelegate {
let clLocationManager = CLLocationManager()
var latitude: AnyObject = 0.000000
var longitude: AnyObject = 0.000000
override func viewDidLoad() {
super.viewDidLoad()
self.nameTextField.delegate = self
clLocationManager.delegate = self
clLocationManager.desiredAccuracy = kCLLocationAccuracyBest
clLocationManager.requestWhenInUseAuthorization()
clLocationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
(placemarks, error) -> Void in
self.clLocationManager.stopUpdatingLocation()
if placemarks!.count > 0 {
self.latitude = (manager.location?.coordinate.latitude)!
self.longitude = (manager.location?.coordinate.longitude)!
print(self.latitude)
print(self.longitude)
} else {
print("Error")
}
})
}
}
谢谢!
尝试将stopUpdatingLocation从reverseGeocodeLocation移到外部
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
(placemarks, error) -> Void in
if placemarks!.count > 0 {
self.latitude = (manager.location?.coordinate.latitude)!
self.longitude = (manager.location?.coordinate.longitude)!
print(self.latitude)
print(self.longitude)
} else {
print("Error")
}
})
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句