CLLocationManager 有时 returns (0, 0) 坐标
CLLocationManager sometimes returns (0, 0) coordinate
我正在使用 CLLocationManager 获取用户坐标。然后我将它发送到我们的网站 API 以获取一些信息。但是最近,API 开始返回由零坐标引起的错误。在错误报告中,我发现一小部分用户开始从 CLLocationManager 获取 (0, 0) 坐标。关键是我们没有改变任何有关地理位置的信息。我有点惊讶,因为这部分已经完美地工作了 2 年了。
地理位置代码是标准的:
locationManager.requestWhenInUseAuthorization()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
//Started returning (0, 0) sometimes
}
我用谷歌搜索了这个问题,但没有结果。有什么猜测吗?
嘿@Edward 将此代码用于定位。 add 还要在 info.plist
中添加密钥
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)
}
嘿,我会检查准确性,请参阅 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]' 往往会产生错误 -> 像我一样保护最后一个位置,你应该没问题。
希望这对您有所帮助。 (:
我正在使用 CLLocationManager 获取用户坐标。然后我将它发送到我们的网站 API 以获取一些信息。但是最近,API 开始返回由零坐标引起的错误。在错误报告中,我发现一小部分用户开始从 CLLocationManager 获取 (0, 0) 坐标。关键是我们没有改变任何有关地理位置的信息。我有点惊讶,因为这部分已经完美地工作了 2 年了。
地理位置代码是标准的:
locationManager.requestWhenInUseAuthorization()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
//Started returning (0, 0) sometimes
}
我用谷歌搜索了这个问题,但没有结果。有什么猜测吗?
嘿@Edward 将此代码用于定位。 add 还要在 info.plist
中添加密钥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)
}
嘿,我会检查准确性,请参阅 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]' 往往会产生错误 -> 像我一样保护最后一个位置,你应该没问题。
希望这对您有所帮助。 (: