提交 263fc61c 编写于 作者: C codexu

feat(h5): 位置更新 API

上级 4635897f
......@@ -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'
......
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<API_TYPE_START_LOCATION_UPDATE> =
{
type: String
}
export const StartLocationUpdateOptions: ApiOptions<API_TYPE_START_LOCATION_UPDATE> =
{
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
......@@ -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",
......
......@@ -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'
......
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 = <API_TYPE_START_LOCATION_UPDATE>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 = <API_TYPE_ON_LOCATION_CHANGE>(
defineOnApi(API_ON_LOCATION_CHANGE, () => {})
)
export const stopLocationUpdate = <API_TYPE_STOP_LOCATION_UPDATE>(
defineAsyncApi(API_STOP_LOCATION_UPDATE, (_, { resolve, reject }) => {
if (watchId) {
navigator.geolocation.clearWatch(watchId)
watchId = 0
resolve()
} else {
reject('stopLocationUpdate:fail')
}
})
)
export const offLocationChange = <API_TYPE_OFF_LOCATION_CHANGE>(
defineOffApi(API_OFF_LOCATION_CHANGE, () => {
stopLocationUpdate()
})
)
export const onLocationChangeError = <API_TYPE_ON_LOCATION_CHANGE_ERROR>(
defineOnApi(API_ON_LOCATION_CHANGE_ERROR, () => {})
)
export const offLocationChangeError = <API_TYPE_OFF_LOCATION_CHANGE_ERROR>(
defineOnApi(API_OFF_LOCATION_CHANGE_ERROR, () => {})
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册