问题:我需要在 viewDidLoad() 中执行 getReports() 之前获取位置坐标。如您所见,我首先在 viewDidLoad() 中调用 startLocationUpdates。不幸的是,调用 startLocationUpdates() 和调用 didUpdateLocations 之间存在延迟。此延迟导致纬度和经度坐标每次在执行 getReports() 之前都为零。
如何确保 didUpdateLocations 在 getReports() 之前被调用,以便正确设置纬度和经度值?
override func viewDidLoad() {
super.viewDidLoad()
self.startLocationUpdates()
// Do any additional setup after loading the view.
self.mapView.delegate = self
if mblnLocationAcquired {
getReports()
}
}
}
func startLocationUpdates() {
var currentLocation = CLLocation()
mLocationManager.desiredAccuracy = kCLLocationAccuracyBest
mLocationManager.delegate = self
//mLocationManager.requestWhenInUseAuthorization()
mLocationManager.requestWhenInUseAuthorization()
mLocationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as CLLocation
self.mdblLat = location.coordinate.latitude
self.mdblLong = location.coordinate.longitude
println("didUpdateLocation \(location.coordinate.latitude), \(location.coordinate.longitude)")
mLocationManager.stopUpdatingLocation()
mblnLocationAcquired = true
}
最佳答案
How can I be sure that didUpdateLocations gets called BEFORE getReports() so that the latitude and longitude values are set properly?
简单。不要调用 getReports()
,直到 纬度和经度设置正确! 第一个 更新一到就关闭更新,这是一个巨大的错误。请记住,在您开始位置更新后,GPS 需要很长时间才能“预热”。您将收到大量初始更新,其中位置毫无意义或相距数英里。您需要使用位置的准确读数,以便在显示任何内容之前等到您有一个好的值。
关于ios - Swift - 从 didUpdateLocations 获取 GPS 坐标需要很长时间才能返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495243/