CLLocationManager.requestLocation() takes about 10 seconds
Asked Answered
V

5

20

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?

Voracity answered 14/9, 2016 at 20:58 Comment(4)
(which may take several seconds). Make sure you update the UI from the main queueTorres
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.Essen
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.Voracity
Related: https://mcmap.net/q/403339/-didupdatelocations-not-called/12484Teplitz
F
16

If you switch out the

locationManager.requestLocation()

for

locationManager.startUpdatingLocation() 

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

Flattop answered 16/6, 2017 at 4:14 Comment(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().Duthie
M
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.

Marrano answered 6/8, 2019 at 9:22 Comment(0)
H
3

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

Haimes answered 24/5, 2020 at 11:11 Comment(0)
E
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.

Elemi answered 23/9, 2018 at 14:9 Comment(0)
K
1

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()
Killam answered 11/4, 2022 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.