-3

I am using CoreLocation in IOS Swift to get the current coordinates but for some cases, it gives me closest to my desired location while sometimes the coordinates are many meters away from my given address. please check my code which I am using is there any other way to get an accurate current location coordinates.?

My Code is :

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

}

Please help me some time current location is accurate and sometimes it's too meters away. Thanks

  • 1
    Assuming you mean "two meters" in the last paragraph that's reasonably accurate for any consumer-level GPS. You can expect to get a position hundreds of meters away under some situations. – PeterJ Aug 22 '20 at 13:27

1 Answers1

1

Assuming the user of your app has allowed you use precise locations you still need to take into account the fact that the location provided is only as accurate as the hardware can provide. It takes time for the location to get accurate and you may see several calls to didUpdateLocations as the system warms up.

You can use horizontalAccuracy and verticalAccuracy on a CLLocation to make decisions on what to do with a position update.

Warren Burton
  • 17,451
  • 3
  • 53
  • 73