@capacitor/geolocation
Geolocation APIは、GPSを使用してデバイスの現在位置を取得および追跡するためのシンプルなメソッドを提供します。利用可能な場合は、高度、方位、速度の情報も含まれます。
Install
npm install @capacitor/geolocation
npx cap sync
iOS
Appleは、位置情報のためにInfo.plistにプライバシー説明を指定することを要求しています:
NSLocationAlwaysAndWhenInUseUsageDescription(Privacy - Location Always and When In Use Usage Description)NSLocationWhenInUseUsageDescription(Privacy - Location When In Use Usage Description)
このCapacitorプラグインはバックグラウンドジオロケーションを直接サポートしていません。ただし、
ion-ios-geolocationに依存しており、
バックグラウンドで位置を報告できます。そのため、AppleはInfo.plistに
NSLocationAlwaysAndWhenInUseUsageDescriptionエントリを含めることを要求しています。この権限
プロンプトはユーザーには表示されないため、NSLocationWhenInUseUsageDescriptionと同じ
説明文字列を安全に使用できます。
XcodeでのiOSパーミッションの設定については、iOSガイドのInfo.plistの設定を参照してください。
Android
このプラグインは、以下のパーミッションをAndroidManifest.xmlに追加する必要があります:
<!-- Geolocation Plugin -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
最初の2つのパーミッションは、大まかな位置と詳細な位置の両方の位置データを要求します。最後の行はオプションですが、アプリがGPSを必要とする場合は必要です。省略しても構いませんが、GPSハードウェアのないデバイスにアプリがインストールされる可能性があることに注意してください。
概算位置(精度は変動しますが、通常は約2キロメートル)だけが必要な場合は、ACCESS_COARSE_LOCATION と <uses-feature> のみを宣言し、位置情報を要求するときに enableHighAccuracy=false を使用できます。
Androidパーミッションの設定の詳細については、Androidガイドのパーミッションの設定を参照してください。
API
For list of error codes, see Errors
getCurrentPosition(...)
getCurrentPosition(options?: PositionOptions | undefined) => Promise<Position>
Get the current GPS location of the device
| Param | Type |
|---|---|
options | |
Returns:
Promise<Position>
Since: 1.0.0
watchPosition(...)
watchPosition(options: PositionOptions, callback: WatchPositionCallback) => Promise<CallbackID>
Set up a watch for location changes. Note that watching for location changes can consume a large amount of energy. Be smart about listening only when you need to.
| Param | Type |
|---|---|
options | |
callback | |
Returns: Promise<string>
Since: 1.0.0
clearWatch(...)
clearWatch(options: ClearWatchOptions) => Promise<void>
Clear a given watch
| Param | Type |
|---|---|
options | |
Since: 1.0.0
checkPermissions()
checkPermissions() => Promise<PermissionStatus>
Check location permissions. Will throw if system location services are disabled.
Returns:
Promise<PermissionStatus>
Since: 1.0.0
requestPermissions(...)
requestPermissions(permissions?: GeolocationPluginPermissions | undefined) => Promise<PermissionStatus>
Request location permissions. Will throw if system location services are disabled.
Not available on web.
| Param | Type |
|---|---|
permissions | |
Returns:
Promise<PermissionStatus>
Since: 1.0.0