问题:我需要在viewDidLoad()中执行getReports()之前获取位置坐标。如您所见,我首先在viewDidLoad()中调用startLocationUpdates。不幸的是,调用startLocationUpdates()和didUpdateLocations之间有一个延迟被调用。此延迟导致每次执行getReports()之前纬度和经度坐标为零。
如何确保在getReports()之前调用didUpdateLocations以便正确设置纬度和经度值?
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
}
答案 0 :(得分:1)
如何确保在getReports()之前调用didUpdateLocations以便正确设置纬度和经度值?
简单。经过正确设置纬度和经度后,请不要致电getReports()
直到!一旦第一次更新到达,就会关闭更新,从而犯下了一个巨大的错误。请记住,在您开始更新位置后,GPS需要很长时间才能“预热”。您将收到许多初始更新,其中位置无意义或数英里之外。您需要使用该位置的准确度读数,以便在显示任何内容之前等到 good 值。