提交 726b8024 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

fix(location update): 修复回调及监听事件触发错误并调整默认坐标系为gcj02

上级 0043093b
......@@ -25,7 +25,7 @@ export const StartLocationUpdateOptions: ApiOptions<API_TYPE_START_LOCATION_UPDA
type(value, params) {
value = (value || '').toLowerCase()
if (coordTypes.indexOf(value) === -1) {
params.type = coordTypes[0]
params.type = coordTypes[1]
} else {
params.type = value
}
......
......@@ -18,66 +18,62 @@ import {
API_ON_LOCATION_CHANGE_ERROR,
} from '@dcloudio/uni-api'
let started = false
let watchId: number = 0
/**
* 开始更新定位
*/
export const startLocationUpdate =
defineAsyncApi<API_TYPE_START_LOCATION_UPDATE>(
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_TYPE_ON_LOCATION_CHANGE>(
API_ON_LOCATION_CHANGE,
() => {}
)
export const stopLocationUpdate = defineAsyncApi<API_TYPE_STOP_LOCATION_UPDATE>(
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_TYPE_ON_LOCATION_CHANGE>(
API_ON_LOCATION_CHANGE,
() => {}
)
export const offLocationChange = defineOffApi<API_TYPE_OFF_LOCATION_CHANGE>(
API_OFF_LOCATION_CHANGE,
() => {
stopLocationUpdate()
}
() => {}
)
export const onLocationChangeError =
......@@ -87,7 +83,7 @@ export const onLocationChangeError =
)
export const offLocationChangeError =
defineOnApi<API_TYPE_OFF_LOCATION_CHANGE_ERROR>(
defineOffApi<API_TYPE_OFF_LOCATION_CHANGE_ERROR>(
API_OFF_LOCATION_CHANGE_ERROR,
() => {}
)
......@@ -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_TYPE_START_LOCATION_UPDATE>(
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_TYPE_ON_LOCATION_CHANGE>(
API_ON_LOCATION_CHANGE,
() => {}
)
export const stopLocationUpdate = defineAsyncApi<API_TYPE_STOP_LOCATION_UPDATE>(
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_TYPE_ON_LOCATION_CHANGE>(
API_ON_LOCATION_CHANGE,
() => {}
)
export const offLocationChange = defineOffApi<API_TYPE_OFF_LOCATION_CHANGE>(
API_OFF_LOCATION_CHANGE,
() => {
stopLocationUpdate()
}
() => {}
)
export const onLocationChangeError =
......@@ -86,7 +96,7 @@ export const onLocationChangeError =
)
export const offLocationChangeError =
defineOnApi<API_TYPE_OFF_LOCATION_CHANGE_ERROR>(
defineOffApi<API_TYPE_OFF_LOCATION_CHANGE_ERROR>(
API_OFF_LOCATION_CHANGE_ERROR,
() => {}
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册