Skip to content

Commit

Permalink
fix(location update): 修复回调及监听事件触发错误并调整默认坐标系为gcj02
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyuWang committed Oct 19, 2022
1 parent 23f1c94 commit 8c312f0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 111 deletions.
85 changes: 40 additions & 45 deletions src/platforms/app-plus/service/api/location/location-change.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,75 @@
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}`
})
})
},
{
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 = []
}
}
124 changes: 58 additions & 66 deletions src/platforms/h5/service/api/location/location-change.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,88 @@
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 = []
}
}

0 comments on commit 8c312f0

Please sign in to comment.