Reputation: 391
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?
Upvotes: 21
Views: 8963
Reputation: 756
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()
Upvotes: 1
Reputation: 61
I changed .desiredAccuracy
to kCLLocationAccuracyKilometer
and my .requestLocation()
now returns the position immediately
Upvotes: 4
Reputation: 2180
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.
Upvotes: 10
Reputation: 36277
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.
Upvotes: 1
Reputation: 482
If you switch out the
locationManager.requestLocation()
for
locationManager.startUpdatingLocation()
then didUpdateLocations
will start firing immediately. This should solve your problem.
Upvotes: 16