LocationManager
TheLocationManager
provides a unified, battery-efficient interface for accessing location data. It allows your application to either subscribe to a continuous stream of location updates or request a single, on-demand location fix.
You access the LocationManager
through the location
property of an AppSession
instance:
How to Get Continuous Location Updates
To get continuous location updates, you must subscribe to the stream and provide a “handler” function that will be called with the data. The method returns anunsubscribe
function that you should call when you no longer need the updates.
Example:
How to Get a Single Location Fix
Use this when you only need the location once, like for tagging a photo or checking the weather.Methods
subscribeToStream()
Subscribes your application to a continuous stream of location data at a specified accuracy tier.options
: An object containing the subscription parameters.accuracy
: The desired accuracy tier. Must be one of the tiers listed below.
handler
: The callback function that will be executed withLocationUpdate
data each time a new location is available.
unsubscribe
function to stop this specific subscription.
getLatestLocation()
Retrieves a single, fresh location fix that meets your specified accuracy requirement. The system is “intelligent”:- If a high-accuracy stream is already active, it will instantly return the latest data from that stream.
- If not, it will check for a recently cached location that meets your accuracy requirement.
- Only as a last resort will it power on the GPS hardware for a new poll.
options
: An object containing the accuracy requirement.accuracy
: The desired accuracy tier.
Promise
that resolves to a single LocationUpdate
object.
unsubscribeFromStream()
Stops your application’s subscription to the continuous location stream. If no other applications are subscribed to a high-accuracy stream, the system will automatically power down the device’s GPS to conserve battery. Note: It is recommended to call the specificunsubscribe
function returned by subscribeToStream
to manage individual handlers. This method serves as a general-purpose way to stop all of your app’s location subscriptions.
Accuracy Tiers
You can choose how accurate you need the location data to be. Higher accuracy uses more battery.Tier Name | Description |
---|---|
realtime | Highest accuracy and frequency, for turn-by-turn navigation. |
high | High accuracy, but not good enough for turn-by-turn |
tenMeters | Accurate to within about 10 meters. |
hundredMeters | Accurate to within about one hundred meters. |
kilometer | Low-power, accurate to the nearest kilometer. |
threeKilometers | Very low-power for large area geofencing. |
reduced | Minimal power usage for approximate location. |