@capacitor/geolocation
Geolocation API 提供了简便的方法,通过 GPS 获取和追踪设备的当前位置,并在可用时提供海拔、朝向和速度等信息。
安装
npm install @capacitor/geolocation
npx cap sync
iOS
Apple 要求在 Info.plist 中为位置信息指定隐私描述:
NSLocationAlwaysUsageDescription(Privacy - Location Always Usage Description)NSLocationWhenInUseUsageDescription(Privacy - Location When In Use Usage Description)
请阅读 iOS 指南 中的 配置 Info.plist 部分,了解更多关于在 Xcode 中设置 iOS 权限的信息。
Android
此 API 需要将以下权限添加到你的 AndroidManifest.xml 文件中:
<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" />
前两个权限用于请求位置数据,包括粗略和精确位置。最后一行是可选的,但如果你应用 必须 使用 GPS 才能运行,则是必需的。你也可以省略它,但请注意,这可能会导致你的应用安装在缺乏 GPS 硬件的设备上。
请阅读 Android 指南 中的 设置权限 部分,了解更多关于设置 Android 权限的信息。
变量
此插件将使用以下项目变量(定义在应用中的 variables.gradle 文件中):
$playServicesLocationVersion版本com.google.android.gms:play-services-location(默认:17.1.0)
示例
import { Geolocation } from '@capacitor/geolocation';
const printCurrentPosition = async () => {
const coordinates = await Geolocation.getCurrentPosition();
console.log('Current position:', coordinates);
};
API
getCurrentPosition(...)
getCurrentPosition(options?: PositionOptions | undefined) => Promise<Position>
获取设备当前的 GPS 位置
| 参数 | 类型 |
|---|---|
options | |
返回值:
Promise<Position>
自: 1.0.0
watchPosition(...)
watchPosition(options: PositionOptions, callback: WatchPositionCallback) => Promise<CallbackID>
设置一个监听器来监听位置变化。请注意,监听位置变化可能会消耗大量电量。请仅在需要时智能地开启监听。
| 参数 | 类型 |
|---|---|
options | |
callback | |
返回值: Promise<string>
自: 1.0.0
clearWatch(...)
clearWatch(options: ClearWatchOptions) => Promise<void>
清除指定的监听
| 参数 | 类型 |
|---|---|
options | |
自: 1.0.0
checkPermissions()
checkPermissions() => Promise<PermissionStatus>
检查位置权限
返回值:
Promise<PermissionStatus>
自: 1.0.0
requestPermissions(...)
requestPermissions(permissions?: GeolocationPluginPermissions | undefined) => Promise<PermissionStatus>
请求位置权限
| 参数 | 类型 |
|---|---|
permissions | |
返回值:
Promise<PermissionStatus>
自: 1.0.0
接口
Position
| 属性 | 类型 | 描述 | 自 |
|---|---|---|---|
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(位置选项)
| 属性 | 类型 | 说明 | 默认值 | 引入版本 |
|---|---|---|---|---|
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 |
ClearWatchOptions(清除监听选项)
| 属性 | 类型 |
|---|---|
id | |
PermissionStatus(权限状态 )
| 属性 | 类型 | 说明 | 引入版本 |
|---|---|---|---|
location | | 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(地理位置插件权限)
| 属性 |
|---|