ガイド
背景位置追跡のチュートリアル
@capgo/background-geolocationを使用
iOSとAndroidのCapacitorアプリ向けのネイティブジオフェンスと背景位置追跡。JavaScriptまたはバックエンドに精度の高い位置更新をストリームし、円形の地域を監視し、JavaScriptまたはバックエンドにジオフェンスのエントリ/エクスィットトランジションを送信する
インストール
bun add @capgo/background-geolocation
bunx cap sync
このプラグインが公開するもの
start- 正確な前景または背景の位置情報の更新をストリームします。stop- 有効な位置追跡を停止します。openSettings- ユーザーが位置許可を修正する必要がある場合に、ネイティブの設定を開きます。setPlannedRoute- ユーザーが計画ルートを離れたときにネイティブの音を再生します。setupGeofencing- ネイティブのジオフェンスのデフォルト値とオプションのトランジションWebhook配信を構成します。addGeofence- iOSまたはAndroidの円形ジオフェンス地域を監視します。removeGeofence/removeAllGeofences- 一部またはすべての登録された地域を監視停止します。getMonitoredGeofences- 監視中の地域識別子の一覧を表示します。geofenceTransitionlistener - アプリが有効なときにエントリーやエクィットイベントを受信します。geofenceErrorlistener - ネイティブの監視エラーをトランジションイベントと別々に処理します。
例の使用方法
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
Location updatesを停止します。
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 POSTトランジションパイロットをWebViewが休止中でも送信できるようにします。
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" />
Full Reference
- GitHub: https://github.com/Cap-go/capacitor-background-geolocation/
- Docs: /docs/plugins/background-geolocation/
Keep going from Using @capgo/background-geolocation
アイシムによるプログルトントを用たようっています。 Using @capgo/background-geolocation プログルトントをコンタートインドに読してます。 @capgo/background-geolocation for the implementation detail in @capgo/background-geolocation, Getting Started プログルトントをコンタートインドに読してます。 Capgo プログルトントのプログルトントに読してます。 Capgo プログルトントのプログルトントに読してます。 Capacitor Plugins by Capgo 実装詳細については Capacitor プラグインの Capgo を参照してください。 プラグインの追加または更新 実装詳細についてはプラグインの追加または更新を参照してください。