Zum Hauptinhalt springen

Hintergrund-Geolocation Capacitor Plugin

Präzises Background-Location-Tracking mit nativem Geofencing für iOS und Android sowie Transition-Webhooks

Installieren

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

Anleitung

Background Geolocation in Capacitor nutzen

Verwendung von @capgo/background-geolocation

Präzise Background Geolocation und natives Geofencing für Capacitor Apps auf iOS und Android. Nutze es für präzise Location-Updates, das Überwachen von Geofences und das Senden von Enter/Exit-Events an JavaScript oder dein Backend.

Installieren

bun add @capgo/background-geolocation
bunx cap sync

Was dieses Plugin bereitstellt

  • start - Präzise Standort-Updates im Vorder- oder Hintergrund streamen.
  • stop - Aktives Location-Tracking stoppen.
  • openSettings - Native Einstellungen öffnen, wenn Nutzer Berechtigungen anpassen müssen.
  • setPlannedRoute - Einen nativen Sound abspielen, wenn der Nutzer die Route verlässt.
  • setupGeofencing - Native Geofence-Defaults und optionale Webhook-Zustellung konfigurieren.
  • addGeofence - Eine kreisförmige iOS- oder Android-Geofence-Region überwachen.
  • removeGeofence / removeAllGeofences - Das Monitoring einer oder aller registrierten Regionen stoppen.
  • getMonitoredGeofences - Liste der überwachten Region-IDs anzeigen.
  • geofenceTransition listener - Enter- und Exit-Events empfangen, während die App aktiv ist.
  • geofenceError listener - Native Monitoring-Fehler separat von Transition-Events behandeln.

Anwendungsbeispiel

start

Rufe diese Methode auf, um Änderungen am Gerätestandort zu verfolgen. Ein Promise signalisiert, dass der Aufruf abgeschlossen ist. Der Callback wird jedes Mal aufgerufen, wenn ein neuer Standort verfügbar ist oder beim Aufruf dieser Methode ein Fehler auftritt. Verlass dich dabei nicht auf das Promise-Rejection.

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

Stoppt die Standort-Updates.

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

await BackgroundGeolocation.stop();

openSettings

Öffnet die Standorteinstellungen des Geräts. Hilfreich, um Nutzer dazu zu bewegen, die Standortdienste zu aktivieren oder Berechtigungen anzupassen.

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

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

setPlannedRoute

Spielt eine Audiodatei ab, wenn der Nutzer von der geplanten Route abweicht. Damit lässt sich ein Sound abspielen (auch im Hintergrund, nur nativ).

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

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

Native Geofencing

Überwache Stores, Baustellen, Lieferzonen, Campusgelände oder Check-in-Bereiche mit nativen iOS- und Android-Geofences. Füge ein HTTP oder HTTPS hinzu url hinzu, damit native code Transition-Payloads per POST senden, während die WebView angehalten ist.

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();

Füge unter Android ACCESS_BACKGROUND_LOCATION nur dann deinem App-Manifest hinzu, wenn du Background Geofencing brauchst:

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

Vollständige Referenz

Weiterlesen: Verwendung von @capgo/background-geolocation

Wenn du es nutzt Verwendung von @capgo/background-geolocation Um die Arbeit an nativen Plugins zu planen, verbinde sie mit @capgo/background-geolocation für die Implementierungsdetails in @capgo/background-geolocation, Erste Schritte für die Implementierungsdetails in Erste Schritte, Capgo Plugin-Verzeichnis für den Produkt-Workflow im Capgo Plugin-Verzeichnis, Capacitor Plugins von Capgo für die Implementierungsdetails in Capacitor Plugins von Capgo, und Plugins hinzufügen oder aktualisieren für die Implementierungsdetails in Plugins hinzufügen oder aktualisieren.

FAQ

Background Geolocation Plugin FAQ

Wie installiere ich das Background Geolocation Plugin in einer Capacitor App?

Führe "bun add @capgo/background-geolocation" (oder "npm install @capgo/background-geolocation") aus und danach "bunx cap sync", damit die iOS- und Android-Projekte die nativen code übernehmen. Importiere es in deiner App code aus "@capgo/background-geolocation".

Funktioniert @capgo/background-geolocation mit React, Vue und Angular?

Ja. Background Geolocation ist ein Capacitor Package und funktioniert daher mit jedem Web-Framework, das in Capacitor läuft, einschließlich Ionic, React, Vue, Angular, Svelte und reinem JavaScript.

Welche Capacitor-Version unterstützt @capgo/background-geolocation?

Capgo Plugins folgen der Major-Version von Capacitor: Nutze die Plugin-Major-Version, die zu deiner Capacitor Major-Version passt (z. B. Plugin v8 bei Capacitor 8). Die Kompatibilitätstabelle im GitHub README listet die gepflegten Versionen auf.

Ist @capgo/background-geolocation kostenlos und Open Source?

Ja. Der code-Code ist auf GitHub unter https://github.com/Cap-go/capacitor-background-geolocation/ öffentlich einsehbar und das Package lässt sich kostenlos aus npm installieren. Bug-Reports und Pull Requests sind willkommen.

Kann ich code mit Background Geolocation ohne App Store Review aktualisieren?

Die Installation oder ein Upgrade des Plugins ändert den nativen code, weshalb ein neuer Store-Build nötig ist. Danach können JavaScript-, HTML- und CSS-Änderungen, die das Plugin aufrufen, sofort per Capgo Live Updates ausgeliefert werden.

Änderungen an Background Geolocation ohne App-Review ausliefern

Sobald das Plugin in deinem Store-Build ist, bringt Capgo mit Live-Updates deine JavaScript-, HTML- und CSS-Änderungen in Minuten zu den Nutzern.

Starte mit Capgo