Using @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 - app 活跃时接收进入和退出事件.geofenceError- listener - 处理本地监控错误和转换事件.
- 示例使用.
start
- 开始监听设备位置的变化时,调用此方法。 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: https://github.com/Cap-go/capacitor-background-geolocation/
- 文档:/docs/plugins/background-geolocation/
继续使用@capgo/background-geolocation
如果您正在使用 使用@capgo/background-geolocation 来规划原生插件工作,连接它到 @capgo/background-geolocation 为 @capgo/background-geolocation 的实现细节 开始使用 为 Getting Started 的实现细节 Capgo Plugin Directory for the product workflow in Capgo Plugin Directory, Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, and 添加或更新插件 为添加或更新插件的实现细节