# uni.onLocationChange(callback)

监听实时地理位置变化事件,需结合 uni.startLocationUpdate 或 uni.startLocationUpdateBackground 使用。

# onLocationChange 兼容性

Web 微信小程序 Android iOS HarmonyOS
x 4.41 x x x

# 参数

名称 类型 必填 默认值 兼容性 描述
callback (result: OnLocationChangeCallbackResult) => void -

# OnLocationChangeCallbackResult 的属性值

名称 类型 必备 默认值 兼容性 描述
latitude number -
纬度,范围为 -90~90,负数表示南纬
longitude number -
经度,范围为 -180~180,负数表示西经
accuracy number -
位置的精确度
altitude number -
高度,单位 m
altitudeAccuracy number -
高度的精确度
speed number -
速度,单位 m/s
horizontalAccuracy number -
水平精度,单位 m
verticalAccuracy number -
垂直精度,单位 m(Android 无法获取,返回 0)

# 返回值

类型
any

# 参见

# uni.offLocationChange(callback)

关闭监听实时位置变化,前后台都停止消息接收。

# offLocationChange 兼容性

Web 微信小程序 Android iOS HarmonyOS
x 4.41 x x x

# 参数

名称 类型 必填 默认值 兼容性 描述
callback (result: OffLocationChangeCallbackResult) => void -

# OffLocationChangeCallbackResult 的属性值

名称 类型 必备 默认值 兼容性 描述
latitude number -
纬度,范围为 -90~90,负数表示南纬
longitude number -
经度,范围为 -180~180,负数表示西经
accuracy number -
位置的精确度
altitude number -
高度,单位 m
altitudeAccuracy number -
高度的精确度
speed number -
速度,单位 m/s
horizontalAccuracy number -
水平精度,单位 m
verticalAccuracy number -
垂直精度,单位 m(Android 无法获取,返回 0)

# 返回值

类型
any

# 参见

# uni.onLocationChangeError(callback)

监听持续定位接口返回失败时触发。

# onLocationChangeError 兼容性

Web 微信小程序 Android iOS HarmonyOS
x 4.41 x x x

# 参数

名称 类型 必填 默认值 兼容性 描述
callback (result: any) => void -

# 返回值

类型
any

# 参见

# uni.offLocationChangeError(callback)

取消注册位置更新错误回调。

# offLocationChangeError 兼容性

Web 微信小程序 Android iOS HarmonyOS
x 4.41 x x x

# 参数

名称 类型 必填 默认值 兼容性 描述
callback (result: any) => void -

# 返回值

类型
any

# 参见

# uni.startLocationUpdate(options)

开启小程序进入前台时接收位置消息。

# startLocationUpdate 兼容性

Web 微信小程序 Android iOS HarmonyOS
x 4.41 x x x

# 参数

名称 类型 必填 默认值 兼容性 描述
options StartLocationUpdateOptions -
名称 类型 必备 默认值 兼容性 描述
type string -
指定坐标系类型,可以是 wgs84 或 gcj02
success (result: StartLocationUpdateSuccess) => void -
接口调用成功的回调函数
fail (result: UniError) => void -
接口调用失败的回调函数
complete (result: any) => void -
接口调用结束的回调函数(调用成功、失败都会执行)

# StartLocationUpdateSuccess 的属性值

名称 类型 必备 默认值 兼容性 描述
name string -
位置名称
address string -
详细地址
latitude number -
纬度,浮点数,范围为-90~90,负数表示南纬
longitude number -
经度,范围为-180~180,负数表示西经
errMsg string -
错误信息

# 返回值

类型
any

# 参见

# uni.stopLocationUpdate(options)

关闭监听实时位置变化,前后台都停止消息接收。

# stopLocationUpdate 兼容性

Web 微信小程序 Android iOS HarmonyOS
x 4.41 x x x

# 参数

名称 类型 必填 默认值 兼容性 描述
options any -

# 返回值

类型
any

# 参见

# uni.startLocationUpdateBackground(option)

# 示例

hello uni-app x

扫码体验(手机浏览器跳转到App直达页)

Template

Script

<template>
  <!-- #ifdef APP -->
  <text>显示简易操作日志(可滚动查看)</text><button size="mini" @click="log=''">清空日志</button>
  <scroll-view style="max-height: 300px;">
    <text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ log }}</text>
  </scroll-view>
  <view class="uni-list">
    <text style="margin-bottom: 4px"> 请选择定位服务提供商:</text>
    <radio-group class="uni-flex uni-row" @change="providerChange" style="flex-wrap: wrap">
      <radio class="uni-list-cell" style="margin-right: 15px" v-for="(item, index) in providerList" :key="item.id"
        :value="item.id" :checked="index === currentSelectedProvider">
        {{ item.name }}
      </radio>
    </radio-group>
  </view>

  <view class="uni-list">
    <text style="margin-bottom: 4px"> 请选择坐标系:</text>
    <radio-group class="uni-flex uni-row" @change="typeChange" style="flex-wrap: wrap">
      <radio class="uni-list-cell" style="margin-right: 15px" v-for="(item, index) in types" :key="item.value"
        :value="item.value" :checked="index === currentSelectedType">
        {{ item.name }}
      </radio>
    </radio-group>
  </view>

  <scroll-view style="flex:1">
  <!-- #endif -->
    <button class="btnstyle" type="primary" @tap="startLocationUpdate" id="startLocationUpdate">点击连续定位</button>
    <button class="btnstyle" type="primary" @tap="startLocationUpdateBackground"
      id="startLocationUpdateBackground">后台点击连续定位</button>
    <button class="btnstyle" type="primary" @tap="stopLocationUpdate" id="stopLocationUpdate">点击关闭定位</button>
    <button class="btnstyle" type="primary" @tap="onLocationChange" id="onLocationChange">onLocationChange</button>
    <button class="btnstyle" type="primary" @tap="offLocationChange" id="offLocationChange">offLocationChange</button>
    <button class="btnstyle" type="primary" @tap="onLocationChangeError"
      id="onLocationChangeError">onLocationChangeError</button>
    <button class="btnstyle" type="primary" @tap="offLocationChangeError"
      id="offLocationChangeError">offLocationChangeError</button>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>



<style>
  .uni-list {
    border-bottom: 0px;
    background-color: transparent;
    margin-left: 10px;
    margin-right: 10px;
    margin-top: 4px;
    margin-bottom: 4px;
  }

  .uni-list-cell {
    position: relative;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .btnstyle {
    margin: 4px;
  }
</style>

# 通用类型

# GeneralCallbackResult

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