ios - 如何在 IOS Swift 中获取准确的当前位置坐标(纬度/经度)
问题描述
我在 IOS Swift 中使用 CoreLocation 来获取当前坐标,但在某些情况下,它让我最接近我想要的位置,而有时坐标距离我给定的地址有很多米。请检查我正在使用的代码是否有其他方法可以获得准确的当前位置坐标。?
我的代码是:
import CoreLocation
var locationManager:CLLocationManager!
var userLat = 0.0
var userLong = 0.0
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled(){
self.locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:
[CLLocation]) {
guard let userLocation :CLLocation = locations.first else{return}
userLat = userLocation.coordinate.latitude
userLong = userLocation.coordinate.longitude
//current location
print("current location latitude is :%@", userLat)
print("current location longitude is :%@", userLong)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(Error: \(error)")
}
请帮助我一段时间当前位置是准确的,有时它太远了。谢谢
解决方案
假设您的应用程序的用户允许您使用精确的位置,您仍然需要考虑这样一个事实,即所提供的位置仅与硬件所能提供的一样准确。定位准确需要时间,并且您可能会didUpdateLocations
在系统预热时看到多个呼叫。
您可以使用horizontalAccuracy
and verticalAccuracy
on aCLLocation
来决定如何处理位置更新。
推荐阅读
- angular - 使用角度在剑道树视图中附加自定义节点
- java - 使用 ExoPlayer 2.8 播放播放列表中的特定文件
- react-native - redux-observable 您在预期流的地方提供了“未定义”
- android-things - 如何在 Android Things 控制台上注册和识别单个设备或我们如何让设备进入通道
- xamarin - Xamarin.iOS:如何更改工具栏中图像的大小
- java - 使用 Scanner 扫描另一个类中的预定义字符串列表,并将它们解析为 java 中的 Ints
- javascript - navigationOptions 没有在标题上显示我的按钮
- html - 如何避免内嵌图标跳转到移动设备的下一行?
- python - 嵌套循环跟踪
- php - 从数据库中查找的最小日期