跳过主要内容
返回插件
@capgo/capacitor-background-geolocation
教程
由 github.com/Cap-go

后台地理位置

使用原生 iOS 和 Android 地理围栏以及过渡 webhook 实现准确的后台位置跟踪

指南

背景地理位置教程

使用@capgo/background-geolocation

Capacitor iOS 和 Android 应用程序的准确背景地理位置和原生地理围栏。使用它来流式传输精确的位置更新、监控圆形区域以及将地理围栏进入/退出转换传递给 JavaScript 或您的后端。

安装

bun add @capgo/background-geolocation
bunx cap sync

此插件暴露了什么

  • start - 流式传输准确的前台或后台位置更新。
  • stop - 停止活动的位置跟踪。
  • openSettings - 打开原生设置以让用户修复位置权限。
  • setPlannedRoute - 当用户离开计划路线时播放原生声音。
  • setupGeofencing - 配置原生地理围栏的默认值和可选的过渡 webhook 交付。
  • addGeofence - 监听一个 iOS 或 Android 地理围栏区域。
  • removeGeofence / removeAllGeofences - 停止监控已注册的区域之一或全部。
  • getMonitoredGeofences - 列出监控的区域标识符。
  • geofenceTransition listener - 在应用程序处于活动状态时接收进入和退出事件。
  • geofenceError listener - 分别处理本机监控错误和过渡事件。

示例用法

start

要开始监听设备的位置变化,请调用此方法。返回一个 Promise 表示它完成了调用。回调将在每次有新位置可用时或在调用此方法时出现错误时被调用。不要依赖 promise 拒绝来处理此问题。

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

停止位置更新。

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

await BackgroundGeolocation.stop();

openSettings

打开设备的位置设置页面。有助于指导用户启用位置服务或调整权限。

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

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

setPlannedRoute

当用户偏离规划路线时播放声音文件。应用于播放声音(在本机背景下也可用,只有原生)。

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

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

原生地理围栏

监控商店、工地、送货区域、校园或签到区域使用原生iOS和Android地理围栏。添加一个HTTP或HTTPS url 让原生code在WebView被挂起时POST过渡负载。

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

在Android上添加 ACCESS_BACKGROUND_LOCATION 只在您需要后台地理围栏时在应用程序清单中添加

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

全局参考