如何在IOS Swift中获得准确的当前位置坐标(纬度/经度)

如何在IOS Swift中获得准确的当前位置坐标(纬度/经度),ios,swift,core-location,cllocation,currentlocation,Ios,Swift,Core Location,Cllocation,Currentlocation,我在IOS Swift中使用CoreLocation来获取当前坐标,但在某些情况下,它使我最接近所需的位置,而有时坐标距离我给定的地址有很多米。请检查我使用的代码是否有其他方法获得准确的当前位置坐标 我的代码是: import CoreLocation var locationManager:CLLocationManager! var userLat = 0.0 var userLong = 0.0 override func viewDidLoad() { super.viewD

我在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
的多次调用

您可以在
CLLocation
上使用
horizontalAccuracy
verticalAccuracy
来决定如何进行位置更新。

假设您在最后一段中的意思是“两米”,这对于任何消费者级别的GPS来说都相当准确。在某些情况下,你可以期望在几百米以外的地方找到一个位置。