【问题标题】:How to get accurate current location coordinates (latitude/longititude) in IOS Swift如何在 IOS Swift 中获取准确的当前位置坐标(纬度/经度)
【发布时间】:2020-12-11 15:32:42
【问题描述】:

我在 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)")

}

请在当前位置准确但有时距离太远的时候帮助我。谢谢

【问题讨论】:

  • 假设您在最后一段中的意思是“两米”,这对于任何消费级 GPS 来说都是相当准确的。在某些情况下,您可以期望获得数百米外的位置。

标签: ios swift core-location cllocation currentlocation


【解决方案1】:

假设您的应用的用户允许您使用精确的位置,您仍然需要考虑这样一个事实,即所提供的位置仅与硬件所能提供的一样准确。定位准确需要一些时间,当系统预热时,您可能会看到多次呼叫didUpdateLocations。

您可以在CLLocation 上使用horizontalAccuracy 和verticalAccuracy 来决定如何处理职位更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多