title: Taro.getLocation(option)

sidebar_label: getLocation

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

注意

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

参考文档

类型

  1. (option: Option) => Promise<SuccessCallbackResult>

参数

Option

参数 类型 必填 说明
altitude string 传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度
complete (res: CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: CallbackResult) => void 接口调用失败的回调函数
highAccuracyExpireTime number 高精度定位超时时间(ms),指定时间内返回最高精度,该值3000ms以上高精度定位才有效果
isHighAccuracy boolean 开启高精度定位
success (result: SuccessCallbackResult) => void 接口调用成功的回调函数
type string wgs84 返回 gps 坐标,gcj02 返回可用于 Taro.openLocation 的坐标

SuccessCallbackResult

参数 类型 说明
accuracy number 位置的精确度
altitude number 高度,单位 m
horizontalAccuracy number 水平精度,单位 m
latitude number 纬度,范围为 -90~90,负数表示南纬
longitude number 经度,范围为 -180~180,负数表示西经
speed number 速度,单位 m/s
verticalAccuracy number 垂直精度,单位 m(Android 无法获取,返回 0)
errMsg string 调用结果

示例代码

  1. Taro.getLocation({
  2. type: 'wgs84',
  3. success: function (res) {
  4. const latitude = res.latitude
  5. const longitude = res.longitude
  6. const speed = res.speed
  7. const accuracy = res.accuracy
  8. }
  9. })

API 支持度

API 微信小程序 H5 React Native
Taro.getLocation ✔️