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

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

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