Installation
- Bundle
- CDN
Despia Editor setup
Foreground tracking is enabled by default. Enable Background Location only if your app needs to keep recording GPS while the user is in another app or has the screen locked, such as run trackers, delivery apps, or navigation. If your use case is a one-time location read or in-app tracking only, skip this setup entirely.Open the Background Location addon
In the Despia Editor, navigate to App > Addons > Background Location.
Enable the addon
Toggle Background Location on. This adds the iOS background modes entitlement and the Android foreground service permission to your next build, both of which are required for the OS to allow GPS to keep running while your app is not in the foreground.
How it works
Calllocation:// to start the native GPS session and stoplocation:// to end it and retrieve the full session array. While tracking is active, every GPS point is delivered to window.onLocationChange in real time and stored locally on the device. When tracking stops, the complete session is returned to your JavaScript for processing.
Parameters
location:// accepts the following query parameters.
| Parameter | Required | Description |
|---|---|---|
buffer | Yes | Minimum seconds between time-based GPS updates. Use 5 for high frequency, 30 or higher for battery-conscious use cases |
server | No | Remote API endpoint that receives each location update as a POST request. Omit if you only need local storage or frontend updates |
movement | No | Distance threshold in centimetres. When set, an additional update fires immediately whenever the device moves this distance, regardless of the buffer timer. Use 100 for 1 metre precision |
Live updates with window.onLocationChange
Define window.onLocationChange before calling location:// to receive every GPS point in real time as the user moves. Each call receives a single location object with an active flag indicating whether tracking is still running.
window.onLocationChange in chronological order so your frontend can reconstruct the full route without any additional code.
High-accuracy tracking
For use cases where each metre counts, combinemovement with a long buffer to get distance-triggered updates as the primary signal and time-based updates as a heartbeat fallback.
horizontalAccuracy before calculating distances. A value below 10 indicates a precise fix.
Server delivery
Whenserver is set, each GPS point is POSTed to your endpoint as it is recorded. Server delivery, window.onLocationChange, and local session storage all run simultaneously and independently. Loss of network does not affect local storage or frontend callbacks.
Location object
Every location point, whether delivered viawindow.onLocationChange, the session array, or server POST, has the following shape.
| Field | Description | Usage |
|---|---|---|
latitude | GPS latitude coordinate | Map rendering, distance calculation |
longitude | GPS longitude coordinate | Map rendering, distance calculation |
timestamp | Unix timestamp when the point was recorded on the device (seconds) | Chronological ordering, elapsed time |
gpsTimestamp | Raw GPS chip timestamp (seconds), may differ slightly from timestamp | High-precision timing |
speed | Speed in metres per second. null if unavailable | Pace display: (1000 / data.speed / 60).toFixed(2) gives min/km |
course | Direction of travel in degrees from 0 to 360. null if unavailable | Rotate a heading arrow: arrow.style.transform = 'rotate(' + data.course + 'deg)' |
altitude | Elevation above sea level in metres | Elevation gain charts |
horizontalAccuracy | Accuracy radius of the lat/lng in metres. Lower is better | Filter noisy points: discard if > 10 |
verticalAccuracy | Accuracy of the altitude measurement in metres | Filter noisy elevation data |
battery | Device battery percentage recorded at the exact moment of each GPS point | Battery drain per route: firstPoint.battery - lastPoint.battery |
active | true while tracking is running. false on the final event when tracking stops | Drive UI state, finalize route |
Tracking state
Usedespia.locationTracking to reflect the current tracking state in your UI. It is set to true when location:// is called and false when stoplocation:// is called.
Calculate distance and analyse movement
Use the Haversine formula to calculate total distance from the session array after stopping.Background tracking notes
Foreground tracking works with no additional permissions and continues as long as the app is in the foreground. To track while the app is backgrounded, follow the Despia Editor setup at the top of this page. Once enabled, Despia manages the iOS blue badge indicator automatically and dismisses it as soon asstoplocation:// is called.
On Android, Despia handles background location delivery across all major manufacturers including Samsung, Huawei, Xiaomi, and OnePlus, which apply aggressive background restrictions by default. No additional configuration is required. If you encounter missing server events on a specific Android device, contact location@despia.com.
Resources
NPM Package
despia-native