提交 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 ...@@ -25,7 +25,7 @@ export const StartLocationUpdateOptions: ApiOptions<API_TYPE_START_LOCATION_UPDA
type(value, params) { type(value, params) {
value = (value || '').toLowerCase() value = (value || '').toLowerCase()
if (coordTypes.indexOf(value) === -1) { if (coordTypes.indexOf(value) === -1) {
params.type = coordTypes[0] params.type = coordTypes[1]
} else { } else {
params.type = value params.type = value
} }
......
...@@ -18,66 +18,62 @@ import { ...@@ -18,66 +18,62 @@ import {
API_ON_LOCATION_CHANGE_ERROR, API_ON_LOCATION_CHANGE_ERROR,
} from '@dcloudio/uni-api' } from '@dcloudio/uni-api'
let started = false
let watchId: number = 0 let watchId: number = 0
/**
* 开始更新定位
*/
export const startLocationUpdate = export const startLocationUpdate =
defineAsyncApi<API_TYPE_START_LOCATION_UPDATE>( defineAsyncApi<API_TYPE_START_LOCATION_UPDATE>(
API_START_LOCATION_UPDATE, API_START_LOCATION_UPDATE,
(_, { resolve, reject }) => { (options, { resolve, reject }) => {
if (plus.geolocation && watchId === 0) { watchId =
watchId = plus.geolocation.watchPosition( watchId ||
plus.geolocation.watchPosition(
(res) => { (res) => {
started = true
UniServiceJSBridge.invokeOnCallback( UniServiceJSBridge.invokeOnCallback(
API_ON_LOCATION_CHANGE, API_ON_LOCATION_CHANGE,
res.coords res.coords
) )
resolve()
}, },
(error) => { (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 { setTimeout(resolve, 100)
UniServiceJSBridge.invokeOnCallback(
API_ON_LOCATION_CHANGE_ERROR,
'onLocationChange:fail'
)
}
resolve()
}, },
StartLocationUpdateProtocol, StartLocationUpdateProtocol,
StartLocationUpdateOptions StartLocationUpdateOptions
) )
export const onLocationChange = defineOnApi<API_TYPE_ON_LOCATION_CHANGE>(
API_ON_LOCATION_CHANGE,
() => {}
)
export const stopLocationUpdate = defineAsyncApi<API_TYPE_STOP_LOCATION_UPDATE>( export const stopLocationUpdate = defineAsyncApi<API_TYPE_STOP_LOCATION_UPDATE>(
API_STOP_LOCATION_UPDATE, API_STOP_LOCATION_UPDATE,
(_, { resolve, reject }) => { (_, { resolve }) => {
if (watchId) { if (watchId) {
plus.geolocation.clearWatch(watchId) plus.geolocation.clearWatch(watchId)
started = false
watchId = 0 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>( export const offLocationChange = defineOffApi<API_TYPE_OFF_LOCATION_CHANGE>(
API_OFF_LOCATION_CHANGE, API_OFF_LOCATION_CHANGE,
() => { () => {}
stopLocationUpdate()
}
) )
export const onLocationChangeError = export const onLocationChangeError =
...@@ -87,7 +83,7 @@ export const onLocationChangeError = ...@@ -87,7 +83,7 @@ export const onLocationChangeError =
) )
export const offLocationChangeError = export const offLocationChangeError =
defineOnApi<API_TYPE_OFF_LOCATION_CHANGE_ERROR>( defineOffApi<API_TYPE_OFF_LOCATION_CHANGE_ERROR>(
API_OFF_LOCATION_CHANGE_ERROR, API_OFF_LOCATION_CHANGE_ERROR,
() => {} () => {}
) )
...@@ -19,19 +19,24 @@ import { ...@@ -19,19 +19,24 @@ import {
} from '@dcloudio/uni-api' } from '@dcloudio/uni-api'
import { translateGeo } from '../../../helpers/location' import { translateGeo } from '../../../helpers/location'
let started = false
let watchId: number = 0 let watchId: number = 0
/**
* 开始更新定位
*/
export const startLocationUpdate = export const startLocationUpdate =
defineAsyncApi<API_TYPE_START_LOCATION_UPDATE>( defineAsyncApi<API_TYPE_START_LOCATION_UPDATE>(
API_START_LOCATION_UPDATE, API_START_LOCATION_UPDATE,
(_, { resolve, reject }) => { (options, { resolve, reject }) => {
if (navigator.geolocation && watchId === 0) { if (!navigator.geolocation) {
watchId = navigator.geolocation.watchPosition( reject()
return
}
watchId =
watchId ||
navigator.geolocation.watchPosition(
(res) => { (res) => {
translateGeo(_?.type, res.coords) started = true
translateGeo(options?.type, res.coords)
.then((coords) => { .then((coords) => {
UniServiceJSBridge.invokeOnCallback( UniServiceJSBridge.invokeOnCallback(
API_ON_LOCATION_CHANGE, API_ON_LOCATION_CHANGE,
...@@ -40,43 +45,48 @@ export const startLocationUpdate = ...@@ -40,43 +45,48 @@ export const startLocationUpdate =
resolve() resolve()
}) })
.catch((error) => { .catch((error) => {
reject(error.message) UniServiceJSBridge.invokeOnCallback(
API_ON_LOCATION_CHANGE_ERROR,
{ errMsg: `onLocationChange:fail ${error.message}` }
)
}) })
}, },
(error) => { (error) => {
reject(error.message) if (!started) {
reject(error.message)
started = true
}
UniServiceJSBridge.invokeOnCallback(API_ON_LOCATION_CHANGE_ERROR, {
errMsg: `onLocationChange:fail ${error.message}`,
})
} }
) )
} setTimeout(resolve, 100)
resolve()
}, },
StartLocationUpdateProtocol, StartLocationUpdateProtocol,
StartLocationUpdateOptions StartLocationUpdateOptions
) )
export const onLocationChange = defineOnApi<API_TYPE_ON_LOCATION_CHANGE>(
API_ON_LOCATION_CHANGE,
() => {}
)
export const stopLocationUpdate = defineAsyncApi<API_TYPE_STOP_LOCATION_UPDATE>( export const stopLocationUpdate = defineAsyncApi<API_TYPE_STOP_LOCATION_UPDATE>(
API_STOP_LOCATION_UPDATE, API_STOP_LOCATION_UPDATE,
(_, { resolve, reject }) => { (_, { resolve }) => {
if (watchId) { if (watchId) {
navigator.geolocation.clearWatch(watchId) navigator.geolocation.clearWatch(watchId)
started = false
watchId = 0 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>( export const offLocationChange = defineOffApi<API_TYPE_OFF_LOCATION_CHANGE>(
API_OFF_LOCATION_CHANGE, API_OFF_LOCATION_CHANGE,
() => { () => {}
stopLocationUpdate()
}
) )
export const onLocationChangeError = export const onLocationChangeError =
...@@ -86,7 +96,7 @@ export const onLocationChangeError = ...@@ -86,7 +96,7 @@ export const onLocationChangeError =
) )
export const offLocationChangeError = export const offLocationChangeError =
defineOnApi<API_TYPE_OFF_LOCATION_CHANGE_ERROR>( defineOffApi<API_TYPE_OFF_LOCATION_CHANGE_ERROR>(
API_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.
先完成此消息的编辑!
想要评论请 注册