From 8c312f00541a94e0876672bee2ab94fcc3da572f Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Wed, 19 Oct 2022 12:05:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(location=20update):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E5=8F=8A=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E9=94=99=E8=AF=AF=E5=B9=B6=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=9D=90=E6=A0=87=E7=B3=BB=E4=B8=BAgcj02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/api/location/location-change.js | 85 ++++++------ .../service/api/location/location-change.js | 124 ++++++++---------- 2 files changed, 98 insertions(+), 111 deletions(-) diff --git a/src/platforms/app-plus/service/api/location/location-change.js b/src/platforms/app-plus/service/api/location/location-change.js index 6a8648ec4..2cfdcaa2b 100644 --- a/src/platforms/app-plus/service/api/location/location-change.js +++ b/src/platforms/app-plus/service/api/location/location-change.js @@ -1,23 +1,26 @@ 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 = [] + } } diff --git a/src/platforms/h5/service/api/location/location-change.js b/src/platforms/h5/service/api/location/location-change.js index 8f4124340..8040b0f8a 100644 --- a/src/platforms/h5/service/api/location/location-change.js +++ b/src/platforms/h5/service/api/location/location-change.js @@ -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 = [] + } } -- GitLab