Skip to main content
@capgo/background-geolocation Location Open source

Background Geolocation Capacitor plugin

Accurate background location tracking with native iOS and Android geofencing plus transition webhooks

Install

bun add @capgo/background-geolocation bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/background-geolocation
  • pnpm add @capgo/background-geolocation
  • yarn add @capgo/background-geolocation

Guide

How to use Background Geolocation in Capacitor

Using @capgo/background-geolocation

Accurate background geolocation and native geofencing for Capacitor apps on iOS and Android. Use it to stream precise location updates, monitor circular regions, and deliver geofence enter/exit transitions to JavaScript or your backend.

Install

bun add @capgo/background-geolocation
bunx cap sync

What This Plugin Exposes

  • start - Stream accurate foreground or background location updates.
  • stop - Stop active location tracking.
  • openSettings - Open native settings when users need to fix location permissions.
  • setPlannedRoute - Play a native sound when the user leaves a planned route.
  • setupGeofencing - Configure native geofence defaults and optional transition webhook delivery.
  • addGeofence - Monitor a circular iOS or Android geofence region.
  • removeGeofence / removeAllGeofences - Stop monitoring one or all registered regions.
  • getMonitoredGeofences - List monitored region identifiers.
  • geofenceTransition listener - Receive enter and exit events while the app is active.
  • geofenceError listener - Handle native monitoring errors separately from transition events.

Example Usage

start

To start listening for changes in the device's location, call this method. A Promise is returned to indicate that it finished the call. The callback will be called every time a new location is available, or if there was an error when calling this method. Don't rely on promise rejection for this.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

await BackgroundGeolocation.start(
  {
    backgroundMessage: "App is using your location in the background",
    backgroundTitle: "Location Service",
    requestPermissions: true,
    stale: false,
    distanceFilter: 10
  },
  (location, error) => {
    if (error) {
      console.error('Location error:', error);
      return;
    }
    if (location) {
      console.log('New location:', location.latitude, location.longitude);
    }
  }
);

stop

Stops location updates.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

await BackgroundGeolocation.stop();

openSettings

Opens the device's location settings page. Useful for directing users to enable location services or adjust permissions.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

// Direct user to location settings
await BackgroundGeolocation.openSettings();

setPlannedRoute

Plays a sound file when the user deviates from the planned route. This should be used to play a sound (in the background too, only for native).

import { BackgroundGeolocation } from '@capgo/background-geolocation';

await BackgroundGeolocation.setPlannedRoute({
  soundFile: "notification.mp3",
  route: [[-74.0060, 40.7128], [-118.2437, 34.0522]]
});

Native geofencing

Monitor stores, job sites, delivery zones, campuses, or check-in areas with native iOS and Android geofences. Add an HTTP or HTTPS url to let native code POST transition payloads while the WebView is suspended.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

await BackgroundGeolocation.setupGeofencing({
  url: 'https://api.example.com/geofences',
  notifyOnEntry: true,
  notifyOnExit: true,
  payload: { userId: '123' },
});

await BackgroundGeolocation.addGeofence({
  identifier: 'warehouse',
  latitude: 40.7128,
  longitude: -74.006,
  radius: 200,
});

const listener = await BackgroundGeolocation.addListener(
  'geofenceTransition',
  (event) => console.log(event.identifier, event.transition),
);

const errorListener = await BackgroundGeolocation.addListener(
  'geofenceError',
  (event) => console.error(event.identifier, event.message),
);

await BackgroundGeolocation.removeGeofence({ identifier: 'warehouse' });
await listener.remove();
await errorListener.remove();

On Android, add ACCESS_BACKGROUND_LOCATION to your app manifest only when you need background geofencing:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Full Reference

Keep going from Using @capgo/background-geolocation

If you are using Using @capgo/background-geolocation to plan native plugin work, connect it with @capgo/background-geolocation for the implementation detail in @capgo/background-geolocation, Getting Started for the implementation detail in Getting Started, Capgo Plugin Directory for the product workflow in Capgo Plugin Directory, Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, and Adding or Updating Plugins for the implementation detail in Adding or Updating Plugins.

FAQ

Background Geolocation plugin FAQ

How do I install the Background Geolocation plugin in a Capacitor app?

Run "bun add @capgo/background-geolocation" (or "npm install @capgo/background-geolocation"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/background-geolocation" in your app code.

Does @capgo/background-geolocation work with React, Vue and Angular?

Yes. Background Geolocation is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/background-geolocation support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/background-geolocation free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-background-geolocation/ and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses Background Geolocation without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship Background Geolocation changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo