CLLocationManager.requestLocation() takes about 10 seconds

蹲街弑〆低调 提交于 2020-05-11 04:32:19

问题


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?


回答1:


If you switch out the

locationManager.requestLocation()

for

locationManager.startUpdatingLocation() 

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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/39499541/cllocationmanager-requestlocation-takes-about-10-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!