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

背景地理位置

精确的背景位置跟踪,使用原生iOS和Android地理围栏,及过渡 webhook

指南

背景地理位置教程

使用@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

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

停止位置更新

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 Plugin Directory 的产品工作流程 Capacitor 由 Capgo 提供的插件 对于在Capacitor插件中实现的细节Capgo 添加或更新插件 对于在添加或更新插件中实现的细节。