Background Geolocation
A Capacitor plugin which lets you receive geolocation updates even while the app is backgrounded. Only iOS and Android platforms are supported.
Usage
import {registerPlugin} from "@capacitor/core";
const BackgroundGeolocation = registerPlugin("BackgroundGeolocation");
BackgroundGeolocation.addWatcher(
{
backgroundMessage: "Cancel to prevent battery drain.",
backgroundTitle: "Tracking You.",
requestPermissions: true,
stale: false,
distanceFilter: 50
},
function callback(location, error) {
if (error) {
if (error.code === "NOT_AUTHORIZED") {
if (window.confirm(
"This app needs your location, " +
"but does not have permission.\n\n" +
"Open settings now?"
)) {
BackgroundGeolocation.openSettings();
}
}
return console.error(error);
}
return console.log(location);
}
).then(function after_the_watcher_has_been_added(watcher_id) {
BackgroundGeolocation.removeWatcher({
id: watcher_id
});
});
{
longitude: 131.723423719132,
latitude: -22.40106297456,
accuracy: 11,
altitude: 65,
altitudeAccuracy: 4,
bearing: 159.60000610351562,
simulated: false,
speed: 23.51068878173828,
time: 1562731602000
}
function guess_location(callback, timeout) {
let last_location;
BackgroundGeolocation.addWatcher(
{
requestPermissions: false,
stale: true
},
function (location) {
last_location = location || undefined;
}
).then(function (id) {
setTimeout(function () {
callback(last_location);
Plugins.BackgroundGeolocation.removeWatcher({id});
}, timeout);
});
}
Typescript support
import {BackgroundGeolocationPlugin} from "@capacitor-community/background-geolocation";
const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>("BackgroundGeolocation");
Installation
Different versions of the plugin support different versions of Capacitor:
Capacitor | Plugin |
---|
v2 | v0.3 |
v3 | v1 |
v4 | v1 |
v5 | v1 |
Read the documentation for v0.3 here.
npm install @capacitor-community/background-geolocation
npx cap update
iOS
Add the following keys to Info.plist.
:
<dict>
...
<key>NSLocationWhenInUseUsageDescription</key>
<string>We need to track your location</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We need to track your location while your device is locked.</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
...
</dict>
Android
Set the the android.useLegacyBridge
option to true
in your Capacitor configuration. This prevent location updates from halting after 5 minutes in the background. See https://capacitorjs.com/docs/config and #89.
On Android 13+, the app needs the POST_NOTIFICATIONS
runtime permission to show the persistent notification informing the user that their location is being used in the background. You may need to request this permission from the user, see https://developer.android.com/develop/ui/views/notifications/notification-permission.
Configration specific to Android can be made in strings.xml
:
<resources>
<string name="capacitor_background_geolocation_notification_channel_name">
Background Tracking
</string>
<string name="capacitor_background_geolocation_notification_icon">
drawable/ic_tracking
</string>
</resources>
Changelog
v1.2.3
- Adds support for Capacitor v4.
v1.2.2
- Prevents location updates from halting on iOS due to extended inactivity.
v1.2.1
- Fixes background location updates for some devices running Android 12.
v1.2.0
- On iOS, the status bar now turns blue whilst the location is being watched in the background. This provides the user a straightforward way to return to the app.
v1.0.4
- Adds the
ACCESS_COARSE_LOCATION
permission. This is required for apps which target Android 12 (API level 31). A preceeding example shows how to add this permission to your app's manifest.
v1.0.0
- BREAKING:
addWatcher
now returns a Promise which resolves to the callback ID, rather than the callback ID itself. - BREAKING: The plugin is imported via Capacitor's
registerPlugin
function, rather than from the Plugins
object. - BREAKING: Drops support for iOS v11 and Capacitor v2.
- Adds support for Capacitor v3.