TruMan1
TruMan1

Reputation: 36058

How to manually trigger CLLocationManager update?

I am monitoring the users location with these configurations:

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
    locationManager.requestAlwaysAuthorization()
    locationManager.startMonitoringSignificantLocationChanges()

I have a switch that allows the user to either enter an address manually or allow for GPS tracking. When toggling from manual to GPS location, I'd like to kick the CLLocationManager to update the users location and call the didUpdateLocations event. Otherwise, it just waits and waits to update when it feels like it. Is manually triggering an update one time possible?

Upvotes: 1

Views: 2354

Answers (3)

André Slotta
André Slotta

Reputation: 14030

from the docs:

startUpdatingLocation

Calling this method several times in succession does not automatically result in new events being generated. Calling stopUpdatingLocation in between, however, does cause a new initial event to be sent the next time you call this method.

so you could stop and immediately restart the location updates to trigger it manually!

Upvotes: 3

Wain
Wain

Reputation: 119021

Start it when you want to start scanning for updates, but not when you want an immediate location.

If you want an immediate location, request it with manager.location. If the response is nil then no location is currently available and you need to wait. If a location is returned you can check the timestamp to determine if you can use it.

Upvotes: 0

matt
matt

Reputation: 534885

Is manually triggering an update one time possible

Yes, but not with startMonitoringSignificantLocationChanges. You will need to call startUpdatingLocation. Note that even then it may take several calls to your delegate, and several seconds, before you get your single usable update.

What I do (example code here) is to watch both the horizontalAccuracy and the timestamp, and stop when I either get a sufficiently accurate location or too much time elapses.

Upvotes: 0

Related Questions