# uni.getLocation(options)

获取当前的地理位置、速度

# 参数

名称 类型 必填 默认值 描述
options GetLocationOptions -
名称 类型 必备 默认值 描述
type "wgs84" | "gcj02" | "gps" wgs84 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于uni.openLocation的坐标,web端需配置定位 SDK 信息才可支持 gcj02
altitude boolean | null false 传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度
geocode boolean | null false 传入 true 会解析地址
highAccuracyExpireTime number | null 3000 高精度定位超时时间(ms),指定时间内返回最高精度,该值3000ms以上高精度定位才有效果
isHighAccuracy boolean | null false 开启高精度定位
success (result: GetLocationSuccess) => void | null - 接口调用成功的回调函数
fail (result: IGetLocationFail) => void | null - 接口调用失败的回调函数
complete (result: any) => void | null - 接口调用结束的回调函数(调用成功、失败都会执行)
# GetLocationSuccess 的属性值
名称 类型 必备 默认值 描述
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 地址信息
# IGetLocationFail 的属性值
名称 类型 必备 默认值 描述
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 - -

# getLocation 兼容性

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、坐标系、隐私和权限等,请仔细阅读下面的参考链接。

# 参见

相关 Bug

# 示例

hello uni-app x

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>


# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 描述
errMsg string - 错误信息