2

I want to get the user's location. It might be approximate position, that's fine. In didUpdateLocations method, i saw two ways to get the coordinates.

  1. Using manager.location.coordinate
  2. Using locations array

Which one should I go for? I am thinking locations array will contain user's recent location as I am starting startUpdatingLocation() as soon as user opens the app.

So which one should i use to get the coordinates? And what does manager.location.coordinate do in this case?

Note: Accuracy of location need not be accurate to 10 meters. Rough estimation is enough.

Here is my code:

func initLocationUpdates() {
    if CLLocationManager.locationServicesEnabled() {
        print("going to get location")
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
    else {
        print("location services disabled")
    }
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    print("locations = \(locValue.latitude) \(locValue.longitude)")

    /*
    let userLocation:CLLocation = locations[0]  // or maybe even locations.last
    let long = userLocation.coordinate.longitude;
    let lat = userLocation.coordinate.latitude;
    print("lat: ", lat, " --- long: ", long)
    */

    locationManager.stopUpdatingLocation()
}
kishorer747
  • 810
  • 1
  • 10
  • 24

1 Answers1

2

I'd prefer the array over the instance variable, because of the way desiredAccuracy works - it doesn't guarantee that the location updates you receive will have the accuracy you request - merely that the system will make its best effort to provide them. Depending on how the location manager behaves, you might get a number of updates in the locations array, with differing levels of accuracy. You can then filter the array to find the most accurate one.

The key point is that if multiple updates arrive in the same didUpdateLocations: call, and you just use manager.location, you might be missing an update. The missing update would definitely not be the most recent one, but it might be the most accurate one received so far.

Since you have set desiredAccuracy to a very high level (ten meters), I'm guessing precision is more important to you than timing - it will take a while for the system to deliver an update of that level of accuracy (and it may never arrive if you are indoors or otherwise blocked from using GPS). So the likelihood of the scenario above occurring is reasonable.

As for the purpose of the instance variable location, the docs suggest that it's intended to be used in certain circumstances when the app restarts:

In iOS 4.0 and later, this property may contain a more recent location object at launch time. Specifically, if significant location updates are running and your app is terminated, this property is updated with the most recent location data when your app is relaunched (and you create a new location manager object). This location data may be more recent than the last location event processed by your app.

Rich Tolley
  • 3,812
  • 1
  • 30
  • 39