提交 8c312f00 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

fix(location update): 修复回调及监听事件触发错误并调整默认坐标系为gcj02

上级 23f1c944
import { invoke } from '../../bridge' import { invoke } from '../../bridge'
const callbackIds = []
const callbackOnErrorIds = []
const callbackOffErrorIds = []
let watchId
/** let successCallbackIds = []
* 开始更新定位 let errorCallbackIds = []
*/ let started = false
export function startLocationUpdate ({ type = 'wgs84' }) { let watchId = 0
watchId = plus.geolocation.watchPosition(
export function startLocationUpdate ({ type = 'gcj02' }, callbackId) {
watchId = watchId || plus.geolocation.watchPosition(
res => { res => {
callbackIds.forEach(callbackId => { started = true
successCallbackIds.forEach(callbackId => {
invoke(callbackId, res.coords) invoke(callbackId, res.coords)
}) })
}, },
error => { error => {
callbackOnErrorIds.forEach(callbackId => { if (!started) {
invoke(callbackId, { errMsg: `startLocationUpdate:fail ${error.message}` })
started = true
}
errorCallbackIds.forEach(callbackId => {
invoke(callbackId, { invoke(callbackId, {
errMsg: 'onLocationChange:fail' + error.message errMsg: `onLocationChange:fail ${error.message}`
}) })
}) })
}, },
...@@ -25,56 +28,48 @@ export function startLocationUpdate ({ type = 'wgs84' }) { ...@@ -25,56 +28,48 @@ export function startLocationUpdate ({ type = 'wgs84' }) {
coordsType: type coordsType: type
} }
) )
setTimeout(() => {
invoke(callbackId, {
errMsg: 'startLocationUpdate:ok'
})
}, 100)
} }
/** export function stopLocationUpdate () {
* 暂停更新定位 if (watchId !== 0) {
* @param {*} callbackId
*/
export function stopLocationUpdate (callbackId) {
if (watchId) {
plus.geolocation.clearWatch(watchId) plus.geolocation.clearWatch(watchId)
} else { started = false
invoke(callbackId, { errMsg: 'stopLocationUpdate:fail' }) watchId = 0
} }
return {} return {}
} }
/**
* 监听更新定位
* @param {*} callbackId
*/
export function onLocationChange (callbackId) { export function onLocationChange (callbackId) {
callbackIds.push(callbackId) successCallbackIds.push(callbackId)
}
/**
* 监听更新定位失败
* @param {*} callbackId
*/
export function onLocationChangeError (callbackId) {
callbackOnErrorIds.push(callbackId)
} }
// 移除实时地理位置变化事件的监听函数
export function offLocationChange (callbackId) { export function offLocationChange (callbackId) {
if (callbackId) { if (callbackId) {
const index = callbackIds.indexOf(callbackId) const index = successCallbackIds.indexOf(callbackId)
if (index >= 0) { if (index >= 0) {
callbackIds.splice(index, 1) successCallbackIds.splice(index, 1)
} else {
callbackOffErrorIds.forEach(callbackId => {
invoke(callbackId, {
errMsg: 'offLocationChange:fail'
})
})
} }
} else { } else {
callbackIds.length = 0 successCallbackIds = []
} }
} }
// 移除实时地理位置变化事件的监听函数 export function onLocationChangeError (callbackId) {
errorCallbackIds.push(callbackId)
}
export function offLocationChangeError (callbackId) { export function offLocationChangeError (callbackId) {
callbackOffErrorIds.push(callbackId) if (callbackId) {
const index = errorCallbackIds.indexOf(callbackId)
if (index >= 0) {
errorCallbackIds.splice(index, 1)
}
} else {
errorCallbackIds = []
}
} }
import { translateGeo } from '../../../helpers/location' import { translateGeo } from '../../../helpers/location'
const { invokeCallbackHandler: invoke } = UniServiceJSBridge const { invokeCallbackHandler: invoke } = UniServiceJSBridge
const callbackIds = [] let successCallbackIds = []
const callbackOnErrorIds = [] let errorCallbackIds = []
const callbackOffErrorIds = [] let started = false
let watchId let watchId = 0
/** export function startLocationUpdate ({ type = 'gcj02' }, callbackId) {
* 开始更新定位 if (!navigator.geolocation) {
*/ return {
export function startLocationUpdate ({ type = 'wgs84' }) { errMsg: 'startLocationUpdate:fail'
if (navigator.geolocation) { }
watchId = navigator.geolocation.watchPosition( }
res => {
translateGeo(type, res.coords) watchId = watchId || navigator.geolocation.watchPosition(
.then((coords) => { res => {
callbackIds.forEach(callbackId => { started = true
invoke(callbackId, coords) translateGeo(type, res.coords)
}) .then((coords) => {
}).catch(error => { successCallbackIds.forEach(callbackId => {
callbackOnErrorIds.forEach(callbackId => { invoke(callbackId, coords)
invoke(callbackId, {
errMsg: 'onLocationChange:fail' + error.message
})
})
}) })
}, }).catch(error => {
error => { errorCallbackIds.forEach(callbackId => {
callbackOnErrorIds.forEach(callbackId => { invoke(callbackId, {
invoke(callbackId, { errMsg: `onLocationChange:fail ${error.message}`
errMsg: 'onLocationChange:fail' + error.message })
}) })
}) })
},
error => {
if (!started) {
invoke(callbackId, { errMsg: `startLocationUpdate:fail ${error.message}` })
started = true
} }
) errorCallbackIds.forEach(callbackId => {
} else { invoke(callbackId, {
callbackOnErrorIds.forEach(callbackId => { errMsg: `onLocationChange:fail ${error.message}`
invoke(callbackId, { })
errMsg: 'onLocationChange:fail device nonsupport geolocation'
}) })
}
)
setTimeout(() => {
invoke(callbackId, {
errMsg: 'startLocationUpdate:ok'
}) })
} }, 100)
} }
/** export function stopLocationUpdate () {
* 暂停更新定位 if (watchId !== 0) {
* @param {*} callbackId
*/
export function stopLocationUpdate (callbackId) {
if (watchId) {
navigator.geolocation.clearWatch(watchId) navigator.geolocation.clearWatch(watchId)
} else { started = false
invoke(callbackId, { errMsg: 'stopLocationUpdate:fail' }) watchId = 0
} }
return {} return {}
} }
/**
* 监听更新定位
* @param {*} callbackId
*/
export function onLocationChange (callbackId) { export function onLocationChange (callbackId) {
callbackIds.push(callbackId) successCallbackIds.push(callbackId)
}
/**
* 监听更新定位失败
* @param {*} callbackId
*/
export function onLocationChangeError (callbackId) {
callbackOnErrorIds.push(callbackId)
} }
// 移除实时地理位置变化事件的监听函数
export function offLocationChange (callbackId) { export function offLocationChange (callbackId) {
if (callbackId) { if (callbackId) {
const index = callbackIds.indexOf(callbackId) const index = successCallbackIds.indexOf(callbackId)
if (index >= 0) { if (index >= 0) {
callbackIds.splice(index, 1) successCallbackIds.splice(index, 1)
} else {
callbackOffErrorIds.forEach(callbackId => {
invoke(callbackId, {
errMsg: 'offLocationChange:fail'
})
})
} }
} else { } else {
callbackIds.length = 0 successCallbackIds = []
} }
} }
// 移除实时地理位置变化事件的监听函数 export function onLocationChangeError (callbackId) {
errorCallbackIds.push(callbackId)
}
export function offLocationChangeError (callbackId) { export function offLocationChangeError (callbackId) {
callbackOffErrorIds.push(callbackId) if (callbackId) {
const index = errorCallbackIds.indexOf(callbackId)
if (index >= 0) {
errorCallbackIds.splice(index, 1)
}
} else {
errorCallbackIds = []
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册