Ios CLLocationManager有时返回(0,0)坐标

Ios CLLocationManager有时返回(0,0)坐标,ios,iphone,swift,cllocationmanager,Ios,Iphone,Swift,Cllocationmanager,我正在使用CLLocationManager获取用户坐标。然后我将其发送到我们的Web API以获取一些信息。但最近,API开始返回由零坐标引起的错误。在错误报告中,我发现一小部分用户开始从CLLocationManager获取(0,0)坐标。关键是我们没有改变任何关于地理位置的事情。我有点惊讶,因为这部分已经完美地工作了2年了 地理位置代码是标准的: locationManager.requestWhenInUseAuthorization() func locationManager(_

我正在使用CLLocationManager获取用户坐标。然后我将其发送到我们的Web API以获取一些信息。但最近,API开始返回由零坐标引起的错误。在错误报告中,我发现一小部分用户开始从CLLocationManager获取(0,0)坐标。关键是我们没有改变任何关于地理位置的事情。我有点惊讶,因为这部分已经完美地工作了2年了

地理位置代码是标准的:

locationManager.requestWhenInUseAuthorization()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    //Started returning (0, 0) sometimes
}

我用谷歌搜索了这个问题,但没有结果。猜猜看

Hey@Edward使用此代码定位。在info.plist中添加密钥

NSAPP安全, N位置始终使用说明, n不使用时的位置说明

//  AppDelegate.swift

 import UIKit


 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate,CLLocationManagerDelegate {

var addressString : String!
var locationManager = CLLocationManager()
var lat : Double!
var long : Double!
var cordinates = CLLocationCoordinate2D()
var searchLocationCordinate = CLLocationCoordinate2D()
var currentLocation: CLLocation!


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

checkLocation()

}

 func checkLocation() {
    if CLLocationManager.authorizationStatus() != .authorizedWhenInUse
    {
        print("requestingautorization")
        locationManager.requestWhenInUseAuthorization()

    } else {
        print("startupdatinglocation")
    }
}




 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let userLocation:CLLocation = locations[0] as CLLocation
    cordinates = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,longitude: userLocation.coordinate.longitude)
    print(cordinates)

}

嘿,我要检查准确性,请参阅Apple文档(@Discussion),该值可以指示您的位置是否有效

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    guard let location = locations.last, location.horizontalAccuracy >= 0 else {
        return

    }
}
Doku:

注意:我从来不会访问像“locations[0]”这样的索引位置,它会产生错误->像我一样保护最后一个位置,你应该会没事的


希望这有帮助。(:

您是如何访问位置的?我们有时也会遇到这个问题(只有很少的用户有这个问题,但对他们来说,这种情况经常发生)。您是否理解了为什么会发生这种情况以及如何解决它?测试水平精度是否有效?这是一个我不知道的信息,谢谢!