@capacitor/geolocation
地理位置定位 API 提供了简单的方法来使用 GPS 获取和跟踪设备的当前位置,以及可用的高度、航向和速度信息。
安装
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相同的描述字符串。
阅读 iOS 指南中的配置 Info.plist以获取有关在 Xcode 中设置 iOS 权限的更多信息
Android
此插件需要将以下权限添加到您的 AndroidManifest.xml 中:
<!-- Geolocation Plugin -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<feature android:name="android.hardware.location.gps" />
前两个权限请求位置数据,包括精确和粗略位置,最后一行是可选的,但如果您的应用_需要_ GPS 才能运行则是必需的。您可以省略它,但请记住, 这可能意味着您的应用将安装在缺少 GPS 硬件的设备上。
阅读 Android 指南中的设置权限以获取有关设置 Android 权限的更多信息。
API
错误代码列表,请参阅错误
getCurrentPosition(...)
getCurrentPosition(options?: PositionOptions | undefined) => Promise<Position>
获取设备的当前 GPS 位置
| Param | Type |
|---|---|
options | |
Returns:
Promise<Position>
Since: 1.0.0
watchPosition(...)
watchPosition(options: PositionOptions, callback: WatchPositionCallback) => Promise<CallbackID>
设置位置变化的监视。请注意,监视位置变化可能会消耗大量能量。请明智地仅在需要时进行监听。
| Param | Type |
|---|---|
options | |
callback | |
Returns: Promise<string>
Since: 1.0.0
clearWatch(...)
clearWatch(options: ClearWatchOptions) => Promise<void>
清除指定的监视
| Param | Type |
|---|---|
options | |
Since: 1.0.0
checkPermissions()
checkPermissions() => Promise<PermissionStatus>
检查位置权限。如果系统位置服务被禁用,将抛出异常。
Returns:
Promise<PermissionStatus>
Since: 1.0.0
requestPermissions(...)
requestPermissions(permissions?: GeolocationPluginPermissions | undefined) => Promise<PermissionStatus>
请求位置权限。如果系统位置服务被禁用,将抛出异常。
在 Web 上不可用。
| Param | Type |
|---|---|
permissions | |
Returns:
Promise<PermissionStatus>
Since: 1.0.0
Interfaces
Position
| Prop | Type | Description | Since |
|---|---|---|---|
timestamp | number | 坐标的创建时间戳 | 1.0.0 |
coords | { latitude: number; longitude: number; accuracy: number; altitudeAccuracy: number | null; altitude: number | null; speed: number | null; heading: number | null; } | GPS 坐标以及数据的准确性 | 1.0.0 |
PositionOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
enableHighAccuracy | boolean | 高精度模式(如 GPS,如果可用)。在 Android 12+ 设备上,如果用户未授予 ACCESS_FINE_LOCATION 权限(可以通过 location 别名检查),则将被忽略。 | false | 1.0.0 |
timeout | number | 等待位置更新的最长时间(毫秒)。 | 10000 | 1.0.0 |
maximumAge | number | 可接受返回的可能缓存位置的最大年龄(毫秒) | 0 | 1.0.0 |
minimumUpdateInterval | number | watchPosition 的最小更新间隔。不要与 interval 混淆。如果位置更新可用的速度快于此间隔,则只有在自上次位置更新以来最小更新间隔已过期时才会发生更新。此参数仅适用于 Android。它对 iOS 或 Web 平台没有影响。 | 5000 | 6.1.0 |
interval | number | 在 watchPosition 中接收位置更新的期望间隔(毫秒)。对于非常低的 interval 值(几秒或更少),平台可能无法保证及时的位置更新 - 它们可能比指定的要长。平台也可能能够以比 interval 更快的速度提供位置更新。您可以使用 minimumUpdateInterval 来控制该行为。为了与版本 7.1.x 向后兼容,如果未传递值,则此参数的默认值为 timeout 的值。此参数仅适用于 Android。它对 iOS 或 Web 平台没有影响。 | | 8.0.0 |
enableLocationFallback | boolean | 是否在 Google Play Service 的位置设置检查失败时回退到 Android 框架的 LocationManager。这可能发生在多种原因 - 例如设备没有 Play Services 或设备没有网络连接(飞行模式)如果设置为 false,失败将被传播给调用者。请注意,LocationManager 可能不如 Google Play Services 实现有效。如果设备处于飞行模式,则仅使用 GPS 提供程序,根据 GPS 信号,返回位置可能需要更长时间。这意味着要在这种情况下接收位置,您可能需要提供更高的超时时间。此参数仅适用于 Android。它对 iOS 或 Web 平台没有影响。 | true | 8.0.0 |
ClearWatchOptions
| Prop | Type |
|---|---|
id | |
PermissionStatus
| Prop | Type | Description | Since |
|---|---|---|---|
location | | 位置别名的权限状态。在 Android 上,它请求/检查 ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION 权限。在 iOS 和 Web 上,它请求/检查位置权限。 | 1.0.0 |
coarseLocation | | coarseLocation 别名的权限状态。在 Android 上,它请求/检查 ACCESS_COARSE_LOCATION。在 Android 12+ 上,用户可以在近似位置(ACCESS_COARSE_LOCATION)或精确位置(ACCESS_FINE_LOCATION)之间进行选择,因此如果应用不需要高精度,可以使用此别名。在 iOS 和 Web 上,它将与 location 别名具有相同的值。 | 1.2.0 |
GeolocationPluginPermissions
| Prop | Type |
|---|---|
permissions | GeolocationPermissionType[] |