ios - Swift - 从 didUpdateLocations 获取 GPS 坐标需要很长时间才能返回值

标签 ios swift gps

问题:我需要在 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/

相关文章:

android - 如何为ibeacons设置坐标?

php - Json 错误 : Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value. }

swift - UIAlertAction 可以触发函数吗?

java - 我的 checkGPS 方法从未运行过,gps 保持 0.0,0.0

android每10分钟检测一次gps位置

ios - 使用 UIImagePickerController 访问照片库后崩溃

ios - 点击手势识别器干扰清除按钮

ios - 菜单内容不会溢出它在 iOS 的 Safari 中的父项

ios - MVC 中的智能行为(不是计算数据)? iOS

ios - 流音频无法与 Avplayer 一起使用