How to get accurate current location coordinates (latitude/longititude) in IOS Swift

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

Add Comment
1 Answer(s)

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.

Answered on September 1, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.