跳过主要内容
返回插件
@capgo/capacitor-background-geolocation
教程
@capgo/capacitor-background-geolocation

后台地理位置

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

指南

背景地理位置教程

在设备上测试

下载Capgo应用,然后扫描QRcode。

背景地理位置插件预览二维码 code

使用 @capgo/background-geolocation

精确的背景地理位置和原生地理围栏功能,适用于 iOS 和 Android 的 Capacitor 应用。您可以使用它来流式传输精确的位置更新、监控圆形区域以及将地理围栏进入/退出转换传递给 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" />

全局参考

从使用@capgo/background-geolocation继续

如果您正在使用 使用@capgo/background-geolocation 来规划原生插件工作,连接它与 @capgo/background-geolocation 查看@capgo/background-geolocation的实现细节 入门指南 Getting Started 中的实现细节 Capgo 插件目录 Capgo 插件目录中的产品工作流程 Capacitor 由 Capgo 提供的插件 了解 Capacitor 由 Capgo 提供的插件的实现细节 添加或更新插件 添加或更新插件的实现细节