【问题标题】:CLLocationManager sometimes returns (0, 0) coordinateCLLocationManager 有时会返回 (0, 0) 坐标
【发布时间】:2018-08-23 12:12:18
【问题描述】:

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

地理位置代码是标准的:

locationManager.requestWhenInUseAuthorization()

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

我用谷歌搜索了这个问题,但没有结果。有什么猜测吗?

【问题讨论】:

  • 你如何访问位置..?
  • 我们有时也会遇到这个问题(只有极少数用户有这个问题,但对他们来说它经常发生)。您是否设法理解为什么会发生这种情况以及如何解决它?测试 Horizo​​ntalAccuracy 有用吗?

标签: ios iphone swift cllocationmanager


【解决方案1】:

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

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

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

    }
}

独库:

https://developer.apple.com/documentation/corelocation/cllocation/1423599-horizontalaccuracy https://developer.apple.com/documentation/corelocation/cllocation/1423550-verticalaccuracy

P.S.:我永远不会访问带有像“locations[0]”这样的索引的位置往往会产生错误 -> 像我一样保护最后一个位置,你应该没问题。

希望这会有所帮助。 (:

【讨论】:

  • 这是一个很好的信息,我不知道,谢谢!
  • 感谢您的提示,但我想指出,检查 horizontalAccuracy 在我的测试中不起作用。相反,如果coordinate.latitude == 0,我会立即从didUpdateLocations 发送returning,因为我认为这永远不会有效。由于我在获得有效位置之前不会调用stopUpdatingLocation(),因此didUpdateLocations 将被调用多次,通常是一次,但在我的(有限)测试中最多调用4 次,这似乎是可以接受的。
【解决方案2】:

嘿@Edward 将此代码用于位置。 add 也在 info.plist 中添加 key

NSAppTransportSecurity, NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription

//  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)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2014-07-12
    相关资源
    最近更新 更多