diff --git a/package.json b/package.json index 1b734534efe3e72291f43b0864fbb19ec61c9577..670b683f42c953043d1611554eedc8db737aff9e 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "devDependencies": { "@babel/core": "^7.17.10", "@babel/preset-env": "^7.16.11", - "@dcloudio/types": "^3.0.14", + "@dcloudio/types": "^3.0.15", "@dcloudio/uni-api": "3.0.0-alpha-3060120220907003", "@dcloudio/uni-app": "3.0.0-alpha-3060120220907003", "@jest/types": "^27.0.2", diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index 5348285619810c986580ab8c9acdec09aaeb5ff6..0de90475aa2d3f4092a6cd1f38a87336cb8bc8bd 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -79,6 +79,7 @@ export * from './protocols/network/uploadFile' export * from './protocols/network/socket' export * from './protocols/location/getLocation' +export * from './protocols/location/locationChange' export * from './protocols/route/route' diff --git a/packages/uni-api/src/protocols/location/locationChange.ts b/packages/uni-api/src/protocols/location/locationChange.ts new file mode 100644 index 0000000000000000000000000000000000000000..202c4326cc02fbb6966b0445331346f04e4e38bf --- /dev/null +++ b/packages/uni-api/src/protocols/location/locationChange.ts @@ -0,0 +1,33 @@ +export const API_START_LOCATION_UPDATE = 'startLocationUpdate' +export type API_TYPE_START_LOCATION_UPDATE = typeof uni.startLocationUpdate +export const API_ON_LOCATION_CHANGE = 'onLocationChange' +export type API_TYPE_ON_LOCATION_CHANGE = typeof uni.onLocationChange +export const API_STOP_LOCATION_UPDATE = 'stopLocationUpdate' +export type API_TYPE_STOP_LOCATION_UPDATE = typeof uni.stopLocationUpdate +export const API_OFF_LOCATION_CHANGE = 'offLocationChange' +export type API_TYPE_OFF_LOCATION_CHANGE = typeof uni.offLocationChange +export const API_OFF_LOCATION_CHANGE_ERROR = 'offLocationChangeError' +export type API_TYPE_OFF_LOCATION_CHANGE_ERROR = typeof uni.offLocationChangeError +export const API_ON_LOCATION_CHANGE_ERROR = 'onLocationChangeError' +export type API_TYPE_ON_LOCATION_CHANGE_ERROR = typeof uni.onLocationChangeError + +const coordTypes = ['wgs84', 'gcj02'] + +export const StartLocationUpdateProtocol: ApiProtocol = + { + type: String + } + +export const StartLocationUpdateOptions: ApiOptions = + { + formatArgs: { + type(value, params) { + value = (value || '').toLowerCase() + if (coordTypes.indexOf(value) === -1) { + params.type = coordTypes[0] + } else { + params.type = value + } + } + } + } \ No newline at end of file diff --git a/packages/uni-h5-vite/lib/api.json b/packages/uni-h5-vite/lib/api.json index 632a64bcc3415ffdceb89dedfc831773610031a1..608314f668c220d763dca7c7beafeb99ad0be213 100644 --- a/packages/uni-h5-vite/lib/api.json +++ b/packages/uni-h5-vite/lib/api.json @@ -82,6 +82,8 @@ "offAppShow", "offCompassChange", "offError", + "offLocationChange", + "offLocationChangeError", "offNetworkStatusChange", "offPageNotFound", "offPushMessage", @@ -94,6 +96,8 @@ "onError", "onGyroscopeChange", "onLocaleChange", + "onLocationChange", + "onLocationChangeError", "onMemoryWarning", "onNetworkStatusChange", "onPageNotFound", @@ -152,10 +156,12 @@ "startAccelerometer", "startCompass", "startGyroscope", + "startLocationUpdate", "startPullDownRefresh", "stopAccelerometer", "stopCompass", "stopGyroscope", + "stopLocationUpdate", "stopPullDownRefresh", "switchTab", "uploadFile", diff --git a/packages/uni-h5/src/service/api/index.ts b/packages/uni-h5/src/service/api/index.ts index 3ee5b8c99687162f46594d4563c120c62b2515c7..e1a4824d5e7481df6359270b5c27c87d3b3b58c6 100644 --- a/packages/uni-h5/src/service/api/index.ts +++ b/packages/uni-h5/src/service/api/index.ts @@ -45,6 +45,7 @@ export * from './network/socket' export * from './location/getLocation' export * from './location/openLocation' export * from './location/chooseLocation' +export * from './location/locationChange' export * from './route/navigateBack' export * from './route/navigateTo' diff --git a/packages/uni-h5/src/service/api/location/locationChange.ts b/packages/uni-h5/src/service/api/location/locationChange.ts new file mode 100644 index 0000000000000000000000000000000000000000..1ce0e085f1dad6a57cf6e7692d3110cfce88f65f --- /dev/null +++ b/packages/uni-h5/src/service/api/location/locationChange.ts @@ -0,0 +1,86 @@ +import { + defineAsyncApi, + defineOnApi, + defineOffApi, + API_START_LOCATION_UPDATE, + API_TYPE_START_LOCATION_UPDATE, + StartLocationUpdateProtocol, + StartLocationUpdateOptions, + API_ON_LOCATION_CHANGE, + API_TYPE_ON_LOCATION_CHANGE, + API_TYPE_STOP_LOCATION_UPDATE, + API_STOP_LOCATION_UPDATE, + API_TYPE_OFF_LOCATION_CHANGE, + API_OFF_LOCATION_CHANGE, + API_TYPE_OFF_LOCATION_CHANGE_ERROR, + API_OFF_LOCATION_CHANGE_ERROR, + API_TYPE_ON_LOCATION_CHANGE_ERROR, + API_ON_LOCATION_CHANGE_ERROR +} from '@dcloudio/uni-api' +import { + translateGeo, +} from '../../../helpers/location' + +let watchId: number = 0; + +/** + * 开始更新定位 + */ +export const startLocationUpdate = defineAsyncApi( + API_START_LOCATION_UPDATE, + (_, { resolve, reject }) => { + if (navigator.geolocation && watchId === 0) { + watchId = navigator.geolocation.watchPosition( + (res) => { + translateGeo(_?.type, res.coords) + .then((coords) => { + UniServiceJSBridge.invokeOnCallback( + API_ON_LOCATION_CHANGE, + coords + ) + resolve() + }) + .catch((error) => { + reject(error.message) + }) + }, + (error) => { + reject(error.message) + } + ) + } + resolve() + }, + StartLocationUpdateProtocol, + StartLocationUpdateOptions +) + +export const onLocationChange = ( + defineOnApi(API_ON_LOCATION_CHANGE, () => {}) +) + +export const stopLocationUpdate = ( + defineAsyncApi(API_STOP_LOCATION_UPDATE, (_, { resolve, reject }) => { + if (watchId) { + navigator.geolocation.clearWatch(watchId) + watchId = 0 + resolve() + } else { + reject('stopLocationUpdate:fail') + } + }) +) + +export const offLocationChange = ( + defineOffApi(API_OFF_LOCATION_CHANGE, () => { + stopLocationUpdate() + }) +) + +export const onLocationChangeError = ( + defineOnApi(API_ON_LOCATION_CHANGE_ERROR, () => {}) +) + +export const offLocationChangeError = ( + defineOnApi(API_OFF_LOCATION_CHANGE_ERROR, () => {}) +)