

English
To monitor real-time location change events, it needs to be used in conjunction with uni.startLocationUpdate
or uni.startLocationUpdateBackground
.
注意
Web平台本API之前调用了腾讯地图的gcj02坐标免费转换接口,该接口从2024年7月18日起被腾讯逐步下线,导致老版本中本API无法使用。请立即升级到 uni-app 4.24版。
升级后注意:
如果运行在微信浏览器中,可以使用微信的jssdk的定位能力。这个是微信向腾讯地图申请的key,开发者无需配置自己的key。
地图厂商的商业授权较贵,如需购买,请点击获取优惠。
Platform Difference Description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 | 元服务 | 小红书小程序 |
---|---|---|---|---|---|---|---|---|---|---|
√(3.6.8+) | √(3.6.8+) | √ | x | √ | √ | x | √ | x | √ | √ |
HarmonyOS |
---|
HBuilderX 4.25 |
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 | 城市编码 | 百度小程序 |
street | String | 街道名称 | |
city | String | 城市名称 | 百度小程序 |
country | String | 国家 | 百度小程序 |
countryCode | String | 国家代码 | 百度小程序 |
province | String | 省份 | 百度小程序 |
streetNumber | String | 街道号码 | 百度小程序 |
district | String | 区 | 百度小程序 |
isFullAccuracy | Boolean | 是不是精确定位信息 | 百度小程序 |
altitudeAccuracy | Number | 海拔的精确度信息 | App |
Notice
uni.stopLocationUpdate
method to cancel the monitoring after you do not need to monitor the changes of the geographic location information.微信小程序
若使用该接口,需要在 app.json 中进行声明,否则将无法正常使用该接口,详情抖音小程序
调用此 API 需要申请高精度权限,具体信息见高精度定位运营规范。HarmonyOS Next平台
调用此 API 需要申请定位权限ohos.permission.APPROXIMATELY_LOCATION
、ohos.permission.LOCATION
,需自行在项目中配置权限。具体位置在 harmony-configs/entry/src/main/module.json5
中的 requestPermissions 字段。该配置文件在构建产物中原始鸿蒙工程相同目录内。Example
uni.onLocationChange(function (res) {
console.log('纬度:' + res.latitude);
});
移除实时地理位置变化事件的监听函数。
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
√(3.6.8+) | √(3.6.8+) | √ | x | √ | √ | x | √ | x |
HarmonyOS |
---|
HBuilderX 4.25 |
**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 |
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 |
HarmonyOS |
---|
HBuilderX 4.25 |
FUNCTION CALLBACK parameter
Parameter Name | Type | Description | Platform Difference Description |
---|---|---|---|
errCode | Number | 错误 | 微信小程序 |
errMsg | String | 错误信息 | 抖音小程序 |
Unregister location update error callback.
Platform Difference Description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
√(3.6.8+) | √(3.6.8+) | √ | x | x | √ | x | x | x |
HarmonyOS |
---|
HBuilderX 4.25 |
**Note: The callback parameters on the App side and the H5 side are required. **
FUNCTION CALLBACK parameter
No return value.
开启应用进入前台时接收位置消息。
Platform Difference Description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
√(3.6.8+) | √(3.6.8+) | √ | x | √ | √ | x | √ | x |
HarmonyOS |
---|
HBuilderX 4.25 |
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),
});
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 |
HarmonyOS |
---|
HBuilderX 4.25 |
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) |
开始监听实时地理位置信息变化事件,应用进入前后台时均接收实时地理位置信息。
Platform Difference Description
App | HarmonyOS Next | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|---|
x | HBuilderX 4.31 | 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 | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
注意
HarmonyOS Next平台
调用此 API 需要申请定位权限ohos.permission.APPROXIMATELY_LOCATION
、ohos.permission.LOCATION
及后台定位权限ohos.permission.LOCATION_IN_BACKGROUND
,需自行在项目中配置权限。具体位置在 harmony-configs/entry/src/main/module.json5
中的 requestPermissions 字段。该配置文件在构建产物中原始鸿蒙工程相同目录内。使用三方定位或者地图服务,需向服务提供商(如:高德地图、百度地图、腾讯地图、谷歌地图)申请商业授权和缴纳费用(5万/年)。
DCloud为开发者争取了福利,可优惠获取高德、腾讯的商业授权。如有需求请发邮件到bd@dcloud.io
(注明你的公司名称、应用介绍、HBuilder账户);你也可以直接通过uni-im
发起在线咨询,在线咨询地址:DCloud地图服务专员。
详见:https://uniapp.dcloud.net.cn/tutorial/app-geolocation.html#lic