From 726b8024d87072dfddd55ed7f1d5fa271a1ca1da Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Wed, 19 Oct 2022 12:05:29 +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 --- .../src/protocols/location/locationChange.ts | 2 +- .../service/api/location/locationChange.ts | 54 ++++++++--------- .../service/api/location/locationChange.ts | 58 +++++++++++-------- 3 files changed, 60 insertions(+), 54 deletions(-) diff --git a/packages/uni-api/src/protocols/location/locationChange.ts b/packages/uni-api/src/protocols/location/locationChange.ts index 381fbdb58..7834bcdd2 100644 --- a/packages/uni-api/src/protocols/location/locationChange.ts +++ b/packages/uni-api/src/protocols/location/locationChange.ts @@ -25,7 +25,7 @@ export const StartLocationUpdateOptions: ApiOptions( API_START_LOCATION_UPDATE, - (_, { resolve, reject }) => { - if (plus.geolocation && watchId === 0) { - watchId = plus.geolocation.watchPosition( + (options, { resolve, reject }) => { + watchId = + watchId || + plus.geolocation.watchPosition( (res) => { + started = true UniServiceJSBridge.invokeOnCallback( API_ON_LOCATION_CHANGE, res.coords ) - resolve() }, (error) => { - reject(error.message) + if (!started) { + reject(error.message) + started = true + } + UniServiceJSBridge.invokeOnCallback(API_ON_LOCATION_CHANGE_ERROR, { + errMsg: `onLocationChange:fail ${error.message}`, + }) }, { - coordsType: _?.type, + coordsType: options?.type, } ) - } else { - UniServiceJSBridge.invokeOnCallback( - API_ON_LOCATION_CHANGE_ERROR, - 'onLocationChange:fail' - ) - } - resolve() + setTimeout(resolve, 100) }, StartLocationUpdateProtocol, StartLocationUpdateOptions ) -export const onLocationChange = defineOnApi( - API_ON_LOCATION_CHANGE, - () => {} -) - export const stopLocationUpdate = defineAsyncApi( API_STOP_LOCATION_UPDATE, - (_, { resolve, reject }) => { + (_, { resolve }) => { if (watchId) { plus.geolocation.clearWatch(watchId) + started = false watchId = 0 - resolve() - } else { - reject('stopLocationUpdate:fail') } + resolve() } ) +export const onLocationChange = defineOnApi( + API_ON_LOCATION_CHANGE, + () => {} +) + export const offLocationChange = defineOffApi( API_OFF_LOCATION_CHANGE, - () => { - stopLocationUpdate() - } + () => {} ) export const onLocationChangeError = @@ -87,7 +83,7 @@ export const onLocationChangeError = ) export const offLocationChangeError = - defineOnApi( + defineOffApi( API_OFF_LOCATION_CHANGE_ERROR, () => {} ) diff --git a/packages/uni-h5/src/service/api/location/locationChange.ts b/packages/uni-h5/src/service/api/location/locationChange.ts index 22070bf14..d595e555a 100644 --- a/packages/uni-h5/src/service/api/location/locationChange.ts +++ b/packages/uni-h5/src/service/api/location/locationChange.ts @@ -19,19 +19,24 @@ import { } from '@dcloudio/uni-api' import { translateGeo } from '../../../helpers/location' +let started = false let watchId: number = 0 -/** - * 开始更新定位 - */ export const startLocationUpdate = defineAsyncApi( API_START_LOCATION_UPDATE, - (_, { resolve, reject }) => { - if (navigator.geolocation && watchId === 0) { - watchId = navigator.geolocation.watchPosition( + (options, { resolve, reject }) => { + if (!navigator.geolocation) { + reject() + return + } + + watchId = + watchId || + navigator.geolocation.watchPosition( (res) => { - translateGeo(_?.type, res.coords) + started = true + translateGeo(options?.type, res.coords) .then((coords) => { UniServiceJSBridge.invokeOnCallback( API_ON_LOCATION_CHANGE, @@ -40,43 +45,48 @@ export const startLocationUpdate = resolve() }) .catch((error) => { - reject(error.message) + UniServiceJSBridge.invokeOnCallback( + API_ON_LOCATION_CHANGE_ERROR, + { errMsg: `onLocationChange:fail ${error.message}` } + ) }) }, (error) => { - reject(error.message) + if (!started) { + reject(error.message) + started = true + } + UniServiceJSBridge.invokeOnCallback(API_ON_LOCATION_CHANGE_ERROR, { + errMsg: `onLocationChange:fail ${error.message}`, + }) } ) - } - resolve() + setTimeout(resolve, 100) }, StartLocationUpdateProtocol, StartLocationUpdateOptions ) -export const onLocationChange = defineOnApi( - API_ON_LOCATION_CHANGE, - () => {} -) - export const stopLocationUpdate = defineAsyncApi( API_STOP_LOCATION_UPDATE, - (_, { resolve, reject }) => { + (_, { resolve }) => { if (watchId) { navigator.geolocation.clearWatch(watchId) + started = false watchId = 0 - resolve() - } else { - reject('stopLocationUpdate:fail') } + resolve() } ) +export const onLocationChange = defineOnApi( + API_ON_LOCATION_CHANGE, + () => {} +) + export const offLocationChange = defineOffApi( API_OFF_LOCATION_CHANGE, - () => { - stopLocationUpdate() - } + () => {} ) export const onLocationChangeError = @@ -86,7 +96,7 @@ export const onLocationChangeError = ) export const offLocationChangeError = - defineOnApi( + defineOffApi( API_OFF_LOCATION_CHANGE_ERROR, () => {} ) -- GitLab