# uni.onLocationChange(FUNCTION CALLBACK)

To monitor real-time location change events, it needs to be used in conjunction with uni.startLocationUpdate or uni.startLocationUpdateBackground.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
√(3.6.8+) √(3.6.8+) x x x

FUNCTION CALLBACK parameter

Parameter Name Type Description Platform Difference Description
latitude Number Latitude, the range is -90~90, negative numbers indicate south latitude.
longitude Number Longitude, the range is -180~180, negative numbers represent west longitude.
speed Number Speed (m/s) H5 does not support
accuracy number accuracy of position
altitude number 高度 (m) H5不支持
verticalAccuracy number 垂直精度 (m) 抖音小程序、快手小程序 Android 无法获取,返回 0
horizontalAccuracy number 水平精度 (m) 抖音小程序不支持
city string 定位到的城市信息 百度小程序、抖音小程序(iOS 不支持)
cityCode String City Code Baidu MiniApp street String Street Name
city String City Name Baidu MiniApp
country String Country Baidu MiniApp
countryCode String Country Code Baidu MiniApp
province String Province Baidu MiniApp
streetNumber String Street Number Baidu MiniApp
district String District Baidu MiniApp
isFullAccuracy Boolean Is it accurate positioning information Baidu MiniApp
altitudeAccuracy Number Altitude accuracy information App

Notice

  • This method will continuously monitor the changes of the geographic location information. It is recommended to call the uni.stopLocationUpdate method to cancel the monitoring after you do not need to monitor the changes of the geographic location information.
  • 微信小程序若使用该接口,需要在 app.json 中进行声明,否则将无法正常使用该接口,详情
  • 抖音小程序调用此 API 需要申请高精度权限,具体信息见高精度定位运营规范

Example

uni.onLocationChange(function (res) {
	console.log('纬度:' + res.latitude);
	console.log('经度:' + res.longitude);
});

# uni.offLocationChange(FUNCTION CALLBACK)

移除实时地理位置变化事件的监听函数。

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
√(3.6.8+) √(3.6.8+) x x x

**Note: The callback parameters on the App side and the H5 side are required. **

FUNCTION CALLBACK parameter

Parameter Name Type Description Platform Difference Description
latitude number Latitude, range [-90, 90], negative numbers indicate south latitude Kuaishou MiniApp
longitude number Longitude, range [-180, 180], negative numbers indicate west longitude Kuaishou MiniApp
speed number speed (m/s) Kuaishou MiniApp
accuracy number Accuracy of location Kuaishou MiniApp
altitude number height (m) Kuaishou MiniApp
verticalAccuracy number Vertical Accuracy (m) (Cannot be obtained by Android, returns 0) Kuaishou MiniApp
horizontalAccuracy number horizontal accuracy (m) Kuaishou MiniApp

# uni.onLocationChangeError(FUNCTION CALLBACK)

Triggered when the monitoring continuous positioning interface returns failure.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
√(3.6.8+) √(3.6.8+) x x x x x

FUNCTION CALLBACK parameter

Parameter Name Type Description Platform Difference Description
errCode Number 错误 微信小程序
errMsg String 错误信息 抖音小程序

# uni.offLocationChangeError(FUNCTION CALLBACK)

Unregister location update error callback.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
√(3.6.8+) √(3.6.8+) x x x x x

**Note: The callback parameters on the App side and the H5 side are required. **

FUNCTION CALLBACK parameter

No return value.

# uni.startLocationUpdate(OBJECT)

Receive location messages when opening the MiniApp and entering the foreground.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
√(3.6.8+) √(3.6.8+) x x x

OBJECT parameter

Parameter Name Type Required Description Platform Difference Description
type String 指定坐标系类型,可以是 wgs84 或 gcj02 微信小程序、抖音小程序
success Function No Callback function for successful interface call
fail Function No Callback function for interface call failure
complete function No The callback function of the end of the interface call (the call will be executed if the call succeeds or fails)
needFullAccuracy Boolean No For the new features of iOS14/Android12 and above, this parameter is ignored in other cases. By default, the host returns precise positioning information if it is precise positioning. Passing in true will force the use of precise positioning information. If the precise positioning permission is not available for iOS14/Android12 and above, the precise positioning authorization popup will pop up. Baidu MiniApp

Example

uni.startLocationUpdate({
  success: res => console.log('开启小程序接收位置消息成功'),
  fail: err => console.error('开启小程序接收位置消息失败:', err),
  complete: msg => console.log('调用开启小程序接收位置消息 API 完成')
});

# uni.stopLocationUpdate(OBJECT)

Turn off the monitoring of real-time location changes, and both the front and back will stop receiving messages.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
√(3.6.8+) √(3.6.8+) x x x

OBJECT parameter

Parameter Name Type Required Description Platform Difference Description
success Function No Callback function for successful interface call
fail Function No Callback function for interface call failure
complete function No The callback function of the end of the interface call (the call will be executed if the call succeeds or fails)

# uni.startLocationUpdateBackground(OBJECT)

Start monitoring the real-time geographic location information change event, and the MiniApp will receive real-time geographic location information when it enters the foreground and background.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
x x x x x x x

OBJECT parameter

Parameter Name Type Required Description Platform Difference Description
type String 指定坐标系类型,可以是 wgs84 或 gcj02 微信小程序、抖音小程序
success Function No Callback function for successful interface call
fail Function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

# 三方定位和地图服务收费说明

使用三方定位或者地图服务,需向服务提供商(如:高德地图、百度地图、腾讯地图、谷歌地图)申请商业授权和缴纳费用(5万/年)。

DCloud为开发者争取了福利,可优惠获取高德、腾讯的商业授权。如有需求请发邮件到bd@dcloud.io(注明你的公司名称、应用介绍、HBuilder账户);你也可以直接通过uni-im发起在线咨询,在线咨询地址:DCloud地图服务专员

详见:https://uniapp.dcloud.net.cn/tutorial/app-geolocation.html#lic