Skip to main content

LocationManager

The LocationManager 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 an unsubscribe 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.
Parameters:
  • 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 with LocationUpdate data each time a new location is available.
Returns: An 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.
Parameters:
  • options: An object containing the accuracy requirement.
    • accuracy: The desired accuracy tier.
Returns: A 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 specific unsubscribe 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.