跳到主要内容
版本:3.x

Taro.getLocation(option)

获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。开启高精度定位,接口耗时会增加,可指定 highAccuracyExpireTime 作为超时时间。

注意

  • 工具中定位模拟使用IP定位,可能会有一定误差。且工具目前仅支持 gcj02 坐标。
  • 使用第三方服务进行逆地址解析时,请确认第三方服务默认的坐标系,正确进行坐标转换。

支持情况:

参考文档

类型

(option: Option) => Promise<SuccessCallbackResult>

参数

参数类型
optionOption

Option

参数类型默认值必填说明
altitudebooleanfalse传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度
complete(res: TaroGeneral.CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(res: TaroGeneral.CallbackResult) => void接口调用失败的回调函数
highAccuracyExpireTimenumber高精度定位超时时间(ms),指定时间内返回最高精度,该值3000ms以上高精度定位才有效果
isHighAccuracybooleanfalse开启高精度定位
success(result: SuccessCallbackResult) => void接口调用成功的回调函数
typestring"wgs84"wgs84 返回 gps 坐标,gcj02 返回可用于 Taro.openLocation 的坐标
needFullAccuracybooleanfalse针对 iOS14/Android12 及以上的新特性,其他情况本参数忽略。默认情况宿主是精确定位就返回精确定位信息。
传入 true 会强制使用精确定位信息,iOS14/Android12 及以上如果没有精确定位权限,会弹出精确定位授权弹框
API 支持度: swan

SuccessCallbackResult

参数类型必填说明
accuracynumber位置的精确度
altitudenumber高度,单位 m
horizontalAccuracynumber水平精度,单位 m
latitudenumber纬度,范围为 -90~90,负数表示南纬
longitudenumber经度,范围为 -180~180,负数表示西经
speednumber速度,单位 m/s
verticalAccuracynumber垂直精度,单位 m(Android 无法获取,返回 0)
streetstring城市名称
API 支持度: swan
cityCodestring国家代码
API 支持度: swan
citystring城市名称
API 支持度: swan, tt
countrystring国家
API 支持度: swan
countryCodestring国家代码
API 支持度: swan
provincestring省份
API 支持度: swan
streetNumberstring街道号码
API 支持度: swan
districtstring
API 支持度: swan
isFullAccuracyboolean是不是精确定位信息
API 支持度: swan
errMsgstring调用结果

示例代码

Taro.getLocation({
type: 'wgs84',
success: function (res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
}
})