指南
如何在 Capacitor 中使用背景地理位置
使用@capgo/背景地理位置
精确的背景地理位置和原生地理围栏功能,适用于Capacitor iOS 和 Android 应用。您可以使用它来流式传输精确的位置更新、监控圆形区域以及将地理围栏进入/退出转换传递给 JavaScript 或您的后端。
安装
bun add @capgo/background-geolocation
bunx cap sync
此插件暴露的内容
start- 流式传输准确的前台或后台位置更新。stop- 停止活动的位置跟踪。openSettings- 打开原生设置以修复用户需要的位置权限。setPlannedRoute- 当用户离开计划路线时播放原生声音。setupGeofencing- 配置原生地理围栏默认值和可选的过渡 webhook 交付。addGeofence- 监控 iOS 或 Android 圆形地理围栏区域。removeGeofence/removeAllGeofences- 停止监控已注册的区域之一或所有区域。getMonitoredGeofences- 列出监控区域的标识符。geofenceTransitionlistener - 应用程序活跃时接收进入和退出事件。geofenceErrorlistener - 分别处理本地监控错误和过渡事件。
示例用法
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" />
全局参考
- GitHub: github.com/Cap-go/capacitor-background-geolocation/
- 文档:/docs/plugins/background-geolocation/
继续使用 @capgo/background-geolocation
如果您正在使用 使用 @capgo/background-geolocation 来规划原生插件工作,连接它与 @capgo/背景地理位置 查看 @capgo/background-geolocation 的实现细节 开始使用 Capgo 插件目录 为产品工作流程在 Capgo 插件目录中 Capacitor 插件由 Capgo 为实现细节在 Capacitor 插件由 Capgo 中 添加或更新插件 为实现细节在添加或更新插件中