@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)
[!NOTE] この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ハードウェアのないデバイスにアプリがインストールされる可能性があることに注意してください。
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
Interfaces
Position
| Prop | Type | Description | Since |
|---|---|---|---|
timestamp | number | Creation timestamp for coords | 1.0.0 |
coords | { latitude: number; longitude: number; accuracy: number; altitudeAccuracy: number | null; altitude: number | null; speed: number | null; heading: number | null; } | The GPS coordinates along with the accuracy of the data | 1.0.0 |
PositionOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
enableHighAccuracy | boolean | High accuracy mode (such as GPS, if available) On Android 12+ devices it will be ignored if users didn't grant ACCESS_FINE_LOCATION permissions (can be checked with location alias). | false | 1.0.0 |
timeout | number | The maximum wait time in milliseconds for location updates. | 10000 | 1.0.0 |
maximumAge | number | The maximum age in milliseconds of a possible cached position that is acceptable to return | 0 | 1.0.0 |
minimumUpdateInterval | number | The minumum update interval for watchPosition. Not to be confused with interval. If location updates are available faster than this interval then an update will only occur if the minimum update interval has expired since the last location update. This parameter is only available for Android. It has no effect on iOS or Web platforms. | 5000 | 6.1.0 |
interval | number | Desired interval in milliseconds to receive location updates in watchPosition. For very low values of interval (a couple seconds or less), the platform may not guarantee timely location updates - they may take longer than specified. The platform may also be able to provide location updates faster than interval. You may use minimumUpdateInterval to control that behavior. For backwards compatiblity with version 7.1.x, if no value is passed, the default value of this parameter is that of timeout. This parameter is only available for Android. It has no effect on iOS or Web platforms. | | 8.0.0 |
enableLocationFallback | boolean | Whether to fall back to the Android framework's LocationManager in case Google Play Service's location settings checks fail. This can happen for multiple reasons - e.g. device has no Play Services or device has no network connection (Airplane Mode) If set to false, failures are propagated to the caller. Note that LocationManager may not be as effective as Google Play Services implementation. If the device's in airplane mode, only the GPS provider is used, which may take longer to return a location, depending on GPS signal. This means that to receive location in such circumstances, you may need to provide a higher timeout. This parameter is only available for Android. It has no effect on iOS or Web platforms. | true | 8.0.0 |
ClearWatchOptions
| Prop | Type |
|---|---|
id | |
PermissionStatus
| Prop | Type | Description | Since |
|---|---|---|---|
location | | Permission state for location alias. On Android it requests/checks both ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions. On iOS and web it requests/checks location permission. | 1.0.0 |
coarseLocation | | Permission state for coarseLocation alias. On Android it requests/checks ACCESS_COARSE_LOCATION. On Android 12+, users can choose between Approximate location (ACCESS_COARSE_LOCATION) or Precise location (ACCESS_FINE_LOCATION), so this alias can be used if the app doesn't need high accuracy. On iOS and web it will have the same value as location alias. | 1.2.0 |
GeolocationPluginPermissions
| Prop | Type |
|---|---|
permissions | GeolocationPermissionType[] |
Type Aliases
WatchPositionCallback
(position: Position | null, err?: any): void
CallbackID
string
PermissionState
'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'
GeolocationPermissionType
'location' | 'coarseLocation'
Errors
The plugin returns specific errors with specific codes on native Android and iOS. Web does not follow this standard for errors.
The following table list all the plugin errors:
| Error code | Platform(s) | Message |
|---|---|---|
| OS-PLUG-GLOC-0002 | Android, iOS | There was en error trying to obtain the location. |
| OS-PLUG-GLOC-0003 | Android, iOS | Location permission request was denied. |
| OS-PLUG-GLOC-0004 | iOS | The 'getCurrentPosition' input parameters aren't valid. |
| OS-PLUG-GLOC-0005 | iOS | The 'watchPosition' input parameters aren't valid. |
| OS-PLUG-GLOC-0006 | iOS | The 'clearWatch' input parameters aren't valid. |
| OS-PLUG-GLOC-0007 | Android, iOS | Location services are not enabled. |
| OS-PLUG-GLOC-0008 | iOS | Application's use of location services was restricted. |
| OS-PLUG-GLOC-0009 | Android | Request to enable location was denied. |
| OS-PLUG-GLOC-0010 | Android, iOS | Could not obtain location in time. Try with a higher timeout. |
| OS-PLUG-GLOC-0011 | Android | Timeout needs to be a positive value. |
| OS-PLUG-GLOC-0012 | Android | WatchId not found. |
| OS-PLUG-GLOC-0013 | Android | WatchId needs to be provided. |
| OS-PLUG-GLOC-0014 | Android | Google Play Services error user resolvable. |
| OS-PLUG-GLOC-0015 | Android | Google Play Services error. |
| OS-PLUG-GLOC-0016 | Android | Location settings error. |
| OS-PLUG-GLOC-0017 | Android | Unable to retrieve location because device has both Network and Location turned off. |