diff --git a/src/platforms/app-plus/service/api/index.js b/src/platforms/app-plus/service/api/index.js index 236303114da018bcff7c893c906f742f615bb144..4e76834bb45314e23add090cbe21f950c01fc6e2 100644 --- a/src/platforms/app-plus/service/api/index.js +++ b/src/platforms/app-plus/service/api/index.js @@ -29,6 +29,7 @@ export * from './file/open-document' export * from './location/choose-location' export * from './location/get-location' export * from './location/open-location' +export * from './location/location-change' export * from './media/audio' export * from './media/choose-image' diff --git a/src/platforms/app-plus/service/api/location/location-change.js b/src/platforms/app-plus/service/api/location/location-change.js new file mode 100644 index 0000000000000000000000000000000000000000..6a8648ec45315d1746d1e5387484a12e9e24cc03 --- /dev/null +++ b/src/platforms/app-plus/service/api/location/location-change.js @@ -0,0 +1,80 @@ +import { invoke } from '../../bridge' +const callbackIds = [] +const callbackOnErrorIds = [] +const callbackOffErrorIds = [] +let watchId + +/** + * 开始更新定位 + */ +export function startLocationUpdate ({ type = 'wgs84' }) { + watchId = plus.geolocation.watchPosition( + res => { + callbackIds.forEach(callbackId => { + invoke(callbackId, res.coords) + }) + }, + error => { + callbackOnErrorIds.forEach(callbackId => { + invoke(callbackId, { + errMsg: 'onLocationChange:fail' + error.message + }) + }) + }, + { + coordsType: type + } + ) +} + +/** + * 暂停更新定位 + * @param {*} callbackId + */ +export function stopLocationUpdate (callbackId) { + if (watchId) { + plus.geolocation.clearWatch(watchId) + } else { + invoke(callbackId, { errMsg: 'stopLocationUpdate:fail' }) + } + return {} +} + +/** + * 监听更新定位 + * @param {*} callbackId + */ +export function onLocationChange (callbackId) { + callbackIds.push(callbackId) +} + +/** + * 监听更新定位失败 + * @param {*} callbackId + */ +export function onLocationChangeError (callbackId) { + callbackOnErrorIds.push(callbackId) +} + +// 移除实时地理位置变化事件的监听函数 +export function offLocationChange (callbackId) { + if (callbackId) { + const index = callbackIds.indexOf(callbackId) + if (index >= 0) { + callbackIds.splice(index, 1) + } else { + callbackOffErrorIds.forEach(callbackId => { + invoke(callbackId, { + errMsg: 'offLocationChange:fail' + }) + }) + } + } else { + callbackIds.length = 0 + } +} + +// 移除实时地理位置变化事件的监听函数 +export function offLocationChangeError (callbackId) { + callbackOffErrorIds.push(callbackId) +}