CLLocationManager last known location
Asked Answered
B

3

14

To get the last know geolocation, I have:

CLLocationManager * locationManager = [[CLLocationManager alloc] init];
CLLocation * lastKnownLocation = [locationManager location];

Does the lastKnownLocation reflect the last location my app has been called back with or does it reflect a global iOS system level last location (which other app or the iOS might have queried for)?

I am doing this instead of actively querying for user's location so I don't waste the user's battery.

If user has not given permission to access location, obviously lastKnownLocation should be nil. My question is specifically for the case where user has given my app permission to access location.

Bipod answered 26/3, 2014 at 23:45 Comment(0)
R
5

Generally you should not simply rely on the existing (if any) value returned from location unless you check its accuracy and time stamp (compared to your requirements). It's better to start startUpdatingLocation until you get a location which does match your accuracy requirements and then either stopUpdatingLocation or switch to stopMonitoringSignificantLocationChanges (if available).

The location returned from location is very dependent upon history, so you can't definitely say one way or the other that it will be. You always need to verify the accuracy for your purposes.

Rosenkranz answered 26/3, 2014 at 23:55 Comment(1)
how about just finding out what city the user is in? i just need location data accurate to maybe 100km. assuming the user has switched on the location manager at least once maybe in a different app, that would mean that i get a sufficient result here, right? i really miss that getLastKnownLocation() from android. link.Foredeck
A
2

I had the same question now in 2023 😅 and I still couldn't find any good response using Apple Docs or Google. I also tried to ask it for ChatGPT and the answer was

The lastLocation variable in your code reflects the last known location that your app has received through the CLLocationManager object. This means that it represents the location that your app has acquired, rather than a system-level location that other apps or the iOS might have queried for.

But I also wanted to make a test using the iOS simulator. I made two apps with different bundle ids.

App A has two buttons each of them with the following functionalities:

  • First button: Ask for a location using locationManager.requestLocation() and print the result in func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]
  • Second button: Print the last location using locationManager.location

App B has only one button with the following functionality:

  • Ask for a location using locationManager.requestLocation() and print the result in func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]

After that, I followed these steps:

  1. Run App A in the iOS simulator
  2. Set simulated location (Features > Location > Custom location) to eg. (-10, -20)
  3. Request a fresh location (First button). The app gives me the following:

new location: [<-10.00000000,-20.00000000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 18/04/2023 10:45:53 Horário Padrão de Brasília]

  1. Request the last location (Second button). The app gives me the following:

last location: Optional(<-10.00000000,-20.00000000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 18/04/2023 10:45:53 Horário Padrão de Brasília)

  1. Run App B in the same iOS simulator

  2. Set simulated location (Features > Location > Custom location) to eg. (30, 40)

  3. Request a fresh location. The app gives me the following:

[<+30.00000000,+40.00000000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 18/04/2023 10:48:44 Horário Padrão de Brasília]

  1. Run app A again

  2. Request the last location again (Second button). The app gives me the following:

last location: Optional(<+30.00000000,+40.00000000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 18/04/2023 10:48:44 Horário Padrão de Brasília)

  1. Change the simulated location (Features > Location > Custom location) to eg. (5, 5). The app gives me the following:

last location: Optional(<+30.00000000,+40.00000000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 18/04/2023 10:48:44 Horário Padrão de Brasília)

So, if the device follows the simulators, locationManager.location should give us a global iOS system level last location.

Appreciate answered 18/4, 2023 at 14:13 Comment(1)
I tested this behavior using a real iPhone and two different applications and could confirm this behaviorAppreciate
B
1

I know this is an old question but for the sake of being complete and maybe others ending up here:

The CLLocationManager class has the location property which returns (according to official Apple docs):

The most recently retrieved user location.

This method will return nil when no location is known.

Burka answered 3/8, 2018 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.