提交 fa37075c 编写于 作者: L lixu

feat(h5): 增加更新定位 API

上级 a5e95f2c
......@@ -46,7 +46,13 @@ const location = [
'getLocation',
'chooseLocation',
'openLocation',
'createMapContext'
'createMapContext',
'onLocationChange',
'onLocationChangeError',
'startLocationUpdate',
'stopLocationUpdate',
'offLocationChange',
'offLocationChangeError'
]
const media = [
......
......@@ -48,7 +48,13 @@
"apiList": {
"uni.getLocation": true,
"uni.openLocation": true,
"uni.chooseLocation": true
"uni.chooseLocation": true,
"uni.onLocationChange": true,
"uni.onLocationChangeError": true,
"uni.startLocationUpdate": true,
"uni.stopLocationUpdate": true,
"uni.offLocationChange": true,
"uni.offLocationChangeError": true
}
}, {
"name": "media",
......@@ -225,7 +231,7 @@
"apiList": {
"uni.createRewardedVideoAd": true,
"uni.createFullScreenVideoAd": true,
"uni.'createInterstitialAd'": true,
"uni.'createInterstitialAd'": true,
"uni.'createInteractiveAd'": true
}
}]
此差异已折叠。
......@@ -317,6 +317,30 @@
]
]
],
"onLocationChange": [
"/platforms/h5/service/api/location/location-change.js",
[]
],
"onLocationChangeError": [
"/platforms/h5/service/api/location/location-change.js",
[]
],
"startLocationUpdate": [
"/platforms/h5/service/api/location/location-change.js",
[]
],
"stopLocationUpdate": [
"/platforms/h5/service/api/location/location-change.js",
[]
],
"offLocationChange": [
"/platforms/h5/service/api/location/location-change.js",
[]
],
"offLocationChangeError": [
"/platforms/h5/service/api/location/location-change.js",
[]
],
"chooseImage": [
"/platforms/h5/service/api/media/choose-image.js",
[
......@@ -431,6 +455,18 @@
[],
true
],
"getWindowInfo": [
"/platforms/h5/service/api/device/get-window-info.js",
[]
],
"getDeviceInfo": [
"/platforms/h5/service/api/device/get-system-info.js",
[]
],
"getAppBaseInfo": [
"/platforms/h5/service/api/device/get-system-info.js",
[]
],
"canIUse": [
"/core/service/api/base/can-i-use.js",
[
......@@ -999,11 +1035,19 @@
]
]
],
"getLaunchOptionsSync": [
"/platforms/h5/service/api/plugin/get-launch-options-sync.js",
[]
],
"getEnterOptionsSync": [
"/platforms/h5/service/api/plugin/get-launch-options-sync.js",
[]
],
"invokePushCallback": [
"/core/service/api/plugin/push.js",
[]
],
"getPushCid": [
"getPushClientId": [
"/core/service/api/plugin/push.js",
[]
],
......
const { invokeCallbackHandler: invoke } = UniServiceJSBridge
const callbackIds = []
const callbackOnErrorIds = []
const callbackOffErrorIds = []
let watchId
/**
* 开始更新定位
*/
export function startLocationUpdate () {
if (navigator.geolocation) {
watchId = navigator.geolocation.watchPosition(
res => {
callbackIds.forEach(callbackId => {
invoke(callbackId, res.coords)
})
},
error => {
callbackOnErrorIds.forEach(callbackId => {
invoke(callbackId, {
errMsg: 'getLocation:fail' + error.message
})
})
}
)
} else {
callbackOnErrorIds.forEach(callbackId => {
invoke(callbackId, {
errMsg: 'getLocation:fail device nonsupport geolocation'
})
})
}
}
/**
* 暂停更新定位
* @param {*} callbackId
*/
export function stopLocationUpdate (callbackId) {
if (watchId) {
navigator.geolocation.clearWatch(watchId)
} else {
invoke(callbackId, { errMsg: 'stopLocationUpdate:fail' })
}
return {}
}
/**
* 监听更新定位
* @param {*} callbackId
*/
export function onLocationChange (callbackId) {
callbackIds.push(callbackId)
}
/**
* 监听更新定位失败
* @param {*} callbackId
*/
export function onLocationChangeError (callbackId) {
callbackOnErrorIds.push(callbackId)
}
// 移除实时地理位置变化事件的监听函数
export function offLocationChange (callbackId) {
if (callbackId) {
const index = callbackIds.indexOf(callbackId)
if (index >= 0) {
callbackIds.splice(index, 1)
} else {
callbackOffErrorIds.forEach(callbackId => {
invoke(callbackId, {
errMsg: 'offLocationChange:fail'
})
})
}
} else {
callbackIds.length = 0
}
}
// 移除实时地理位置变化事件的监听函数
export function offLocationChangeError (callbackId) {
callbackOffErrorIds.push(callbackId)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册