2

I need to make an api call based on the current user's location when the view starts.

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 50
        locationManager.startUpdatingLocation()
    }

The issue is that the delegate locationManager(didUpdateLocations) gets called 3 times. Based on the answer How to stop multiple times method calling of didUpdateLocations() in ios it seems like it does that to get an increasingly accurate fix on the user's location.

I only want to call the api call once per significant Location change. (We don't need to do another search for nearby waypoints until the user has moved 50 meters).

The other stackoverflow answers shows how to only respond to the first location update and ignore the others in a short time interval. However it seems like I should use the most accurate of the initial location updates (the 3rd one in this case).

How can I implement such that it uses the most accurate of the initial burst of location updates. Does it always update 3 times reliably? Is there some other delegate method for when it has finished calibrating more accurate location updates?

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • Use a dispatch group to wait until the 3rd call ? You should have a timer based fall-back just in case it never gets called. – nathan Aug 02 '17 at 21:36
  • If you just want to get the current location and not continuous updates then you could use the `requestLocation()` method which does what you want here. – dan Aug 02 '17 at 21:38
  • @dan does requestLocation do the same kind of callibration where it makes multiple updates behind the scenes before emiting one update event? i.e. does it have the same level of accuracy as the 3rd update in the startUpdating scenario? – MonkeyBonkey Aug 02 '17 at 21:56
  • I personally wouldn't rely on the "3rd" update. The speed and number of calls necessary will vary based upon unpredictable environmental factors. I'd keep listening until (a) a non-negative `horizontalAccuracy` for the `CLLocation` drops to a level that is sufficient for your use case or (b) until a certain amount of time has passed, whichever happens first. Or just use `requestLocation()`. – Rob Aug 02 '17 at 21:59
  • @MonkeyBonkey The documentation has a good explanation of what it does. It attempts to find a location based on the `desiredAccuracy` you set on the location manager and if it takes longer than 10 seconds it returns the most accurate one it could find. It only emits one update event – dan Aug 02 '17 at 22:02

0 Answers0