ガイド
__CAPGO_KEEP_0__のバックグラウンド地理位置のチュートリアル
capgo/background-geolocationを使用する
CapacitorアプリケーションにiOSとAndroidで正確なバックグラウンド地理位置とネイティブのジオフェンス機能を提供します。精度の高い位置情報のストリーミング、円形の地域の監視、JavaScriptまたはバックエンドに送信されるジオフェンスのエントリ/エクィットのトランジションを使用してください。
インストール
bun add @capgo/background-geolocation
bunx cap sync
このプラグインが公開するもの
start- 正確な前景またはバックグラウンドの位置情報の更新をストリームします。stop- 有効な位置追跡を停止します。openSettings- ユーザーが位置の許可を修正する必要があるときに、ユーザーにネイティブの設定を開きます。setPlannedRoute- ユーザーが計画ルートを離れたときにネイティブの音を再生します。setupGeofencing- 自動で地理フェンスのデフォルト値とオプションのトランジションWebhookの送信を設定します。addGeofence- iOSまたはAndroidの円形地理フェンス地域を監視します。removeGeofence/removeAllGeofences- 登録された地域の1つまたはすべてを監視停止します。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にPOSTトランジションパイロードを送信するWebビューが停止している場合にのみ、ネイティブのcodeを使用してください。
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/