简体中文
获取当前的地理位置、速度
名称 | 类型 | 必填 | 默认值 | 描述 | |||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | GetLocationOptions | 是 | - | ||||||||||||||||||||||||||||||||||||||||||||||
|
名称 | 类型 | 必备 | 默认值 | 描述 |
---|---|---|---|---|
latitude | number | 是 | 0 | 纬度,浮点数,范围为-90~90,负数表示南纬 |
longitude | number | 是 | 0 | 经度,范围为-180~180,负数表示西经 |
speed | number | 是 | 0 | 速度,浮点数,单位m/s |
accuracy | number | 是 | - | 位置的精确度 |
altitude | number | 是 | 0 | 高度,单位 m |
verticalAccuracy | number | 是 | 0 | 垂直精度,单位 m(Android 无法获取,返回 0) |
horizontalAccuracy | number | 是 | 0 | 水平精度,单位 m |
address | any | null | 否 | null | 地址信息 |
名称 | 类型 | 必备 | 默认值 | 描述 |
---|---|---|---|---|
errCode | 1505004 | 1505005 | 1505021 | 1505022 | 1505023 | 1505024 | 1505025 | 1505026 | 是 | - | 错误码 - 1505004 缺失权限 - 1505005 缺失高精度权限授权(iOS特有) - 1505021 超时 - 1505022 不支持的定位类型 - 1505023 不支持逆地理编码 - 1505024 没有找到具体的定位引擎,请定位开关是否已打开 - 1505025 逆地理编码捕获失败 - 1505026 捕获定位失败 |
errSubject | string | 是 | - | 统一错误主题(模块)名称 |
data | any | null | 否 | - | 错误信息中包含的数据 |
cause | Error | null | 否 | - | 源错误信息,可以包含多个错误,详见SourceError |
errMsg | string | 是 | - | - |
Android | iOS | web |
---|---|---|
3.9.0 | x | 4.0 |
uni-app x的标准基座定位仅支持wgs84坐标系、不支持逆地址解析、且某些老型号国产Android机因gms问题不支持系统定位、国产Rom可能不支持高度信息。
如需更强的定位能力,需加载专业定位sdk。
真机运行基座不包含三方定位sdk。
三方定位sdk方面,暂不支持高德、百度,但支持腾讯定位。
可下载腾讯定位插件,在插件中配置key打包后生效。
上述腾讯定位插件属于ext api插件,引用到工程后,会覆盖uni.getLocation的实现,替换掉系统定位。
如需其他定位,请在插件市场搜索定位相关的uts插件。
获取手机端app是否拥有定位权限,请使用API uni.getAppAuthorizeSetting
不管系统定位、还是三方sdk定位,都有很多注意事项,包括gms、坐标系、隐私和权限等,请仔细阅读下面的参考链接。
Template
Script
<template>
<page-head :title="title"></page-head>
<view style="padding: 4px;">
<text class="hello-text">
定位功能默认调用操作系统定位API实现。\n
部分手机因gms兼容不好可能导致无法定位。\n
gcj国标、逆地理信息等功能需三方sdk定位。如果需要类似能力可以下载腾讯定位插件,打包自定义基座。参考示例:</text>
<u-link :href="'https://ext.dcloud.net.cn/plugin?id=14569'" :text="'https://ext.dcloud.net.cn/plugin?id=14569'" :inWhiteList="true"></u-link>
</view>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-list">
<radio-group @change="radioChange">
<radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
:class="index < items.length - 1 ? 'uni-list-cell-line': ''" :value="item.value"
:checked="index === current">
{{item.name}}
</radio>
</radio-group>
</view>
<view class="uni-list-cell uni-list-cell-pd">
<view class="uni-list-cell-db">高度信息</view>
<switch :checked="altitudeSelect" @change="altitudeChange" />
</view>
<view class="uni-list-cell uni-list-cell-pd">
<view class="uni-list-cell-db">开启高精度定位</view>
<switch :checked="isHighAccuracySelect" @change="highAccuracySelectChange" />
</view>
<view class="uni-list-cell uni-list-cell-pd">
<view class="uni-list-cell-db">是否解析地址信息</view>
<switch :checked="geocodeSelect" @change="geocodeChange" />
</view>
<text>{{exeRet}}</text>
<view class="uni-btn-v">
<button class="uni-btn" type="default" @tap="getLocationTap">获取定位</button>
</view>
</view>
</template>
名称 | 类型 | 必备 | 默认值 | 描述 |
---|---|---|---|---|
errMsg | string | 是 | - | 错误信息 |