18

CLLocationManager.requestLocation() takes around 10 seconds to fire didUpdateLocations event.

Here are the attributes set for the CLLocationManager

let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 10
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()

As per the documentation this can take several seconds.

This method returns immediately. Calling it causes the location manager to obtain a location fix (which may take several seconds) and call the delegate’s locationManager(_:didUpdateLocations:) method with the result.

But can this take 10 long seconds? Or am I missing something?

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • (which may take several seconds). Make sure you update the UI from the main queue – Leo Dabus Sep 14 '16 at 20:59
  • It takes time to get an accurate location if the GPS isn't already run in. If a location can't be found it may take longer than 10 seconds before giving up. – Paulw11 Sep 14 '16 at 21:09
  • 8
    Thanks for all the comments. @SausageMachine I'm not complaining, I'm wondering how apps like Google Maps get location around 3 seconds using the same `While using` location access level. @All genius down voters, I'm asking this because i haven't seen an app taking this much of time to get location. Please enlighten me. – Yasas Karunarathna Sep 15 '16 at 07:11
  • Related: https://stackoverflow.com/q/19624719/12484 – Jon Schneider Sep 18 '20 at 14:12

5 Answers5

16

If you switch out the

locationManager.requestLocation()

for

locationManager.startUpdatingLocation() 

then didUpdateLocations will start firing immediately. This should solve your problem.

bradchattergoon
  • 452
  • 4
  • 14
  • 1
    Thanks, @bradchattergoon. I'm doing this every time the app goes back to the foreground. For my use case, this was not sufficient, so I've added locationManager.stopUpdatingLocation() before locationManager.startUpdatingLocation(). – IvanMih Jan 07 '21 at 13:22
10

TL;DR

requestLocation() is a convenient method provided by Apple which under the hood will run startUpdatingLocation() , retrieve multiple location data, and select the most accurate one to pass to delegate, and call stopUpdatingLocation()

This process can take up to 10 seconds (which is around the timeout limit) if it can't decide which location data is the best.

Kwnstantinos Nikoloutsos
  • 1,832
  • 4
  • 18
  • 34
1

I think you have a false premise. That Google is always faster. I'm guessing that your building app from scratch and the app has no access to cache. Otherwise GoogleMaps can also sometimes take more than 3 seconds. Obviously I don't know the exact specifics but I just think when you're using GoogleMaps you're using it as a user and now when you're developing your own app you're thinking about it as a developer ie you're being more meticulous about it.

Also to have the best of comparisons make sure you set your desiredAccuracy to BestForNavigation, distanceFilter to 0 and activityType to .automotive. That's normally what navigation apps are doing.

Leo's comment is also important: Make sure you update the UI from the main queue

And as mentioned by both highly experienced in Core-Location users: programmer and Paulw11:

When you call startUpdatingLocation on the location manager you must give it time to get a position. You should not immediately call stopUpdatingLocation. We let it run for a maximum of 10 seconds or until we get a non-cached high accuracy location.

mfaani
  • 33,269
  • 19
  • 164
  • 293
1

I changed .desiredAccuracy to kCLLocationAccuracyKilometer and my .requestLocation() now returns the position immediately

Atul
  • 698
  • 4
  • 13
0

I found the following line in my Console logs:

Ignoring requestLocation due to ongoing location.

No idea what's going on. My app doesn't call startUpdatingLocation() at all. But when I keep the app running I can see that locationManager(_:didUpdateLocations:) is called periodically.

Once I put stopUpdatingLocation() in front of the requestLocation() it's as fast as expected:

locationManager.stopUpdatingLocation()
locationManager.requestLocation()
Dominik
  • 706
  • 7
  • 22