diff --git a/lib/apis.js b/lib/apis.js index c5a83ff0f16f8202946b7411784fda6a4979489b..4bbf77c2e3f588b4034c68d9647e03ff96e5fcfe 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -77,6 +77,7 @@ const device = [ 'startAccelerometer', 'stopAccelerometer', 'onCompassChange', + 'offCompassChange', 'startCompass', 'stopCompass', 'onGyroscopeChange', diff --git a/src/core/service/api/device/compass.js b/src/core/service/api/device/compass.js deleted file mode 100644 index 4c9aa58360f7f03a6a5fa68061819dbba81e849d..0000000000000000000000000000000000000000 --- a/src/core/service/api/device/compass.js +++ /dev/null @@ -1,48 +0,0 @@ -import { - invoke -} from 'uni-core/service/bridge' - -import { - onMethod, - invokeMethod -} from '../../platform' - -const callbacks = [] - -onMethod('onCompassChange', function (res) { - callbacks.forEach(callbackId => { - invoke(callbackId, res) - }) -}) - -let isEnable = false -/** - * 监听加速度 - * @param {*} callbackId - */ -export function onCompassChange (callbackId) { - // TODO 当没有 start 时,添加 on 需要主动 start? - callbacks.push(callbackId) - if (!isEnable) { - startCompass() - } -} - -export function startCompass ({ - interval // TODO -} = {}) { - if (isEnable) { - return - } - isEnable = true - return invokeMethod('enableCompass', { - enable: true - }) -} - -export function stopCompass () { - isEnable = false - return invokeMethod('enableCompass', { - enable: false - }) -} diff --git a/src/platforms/app-plus/service/api/device/compass.js b/src/platforms/app-plus/service/api/device/compass.js index 87bcb3bb22d91917ab76eeae224516c281bf3c19..668c594df738fcc8bdb739493440c3d577fe8916 100644 --- a/src/platforms/app-plus/service/api/device/compass.js +++ b/src/platforms/app-plus/service/api/device/compass.js @@ -3,53 +3,59 @@ import { } from '../constants' import { - getLastWebview -} from '../util' - -import { - publish + invoke } from '../../bridge' -let watchOrientationId = false -let isWatchOrientation = false +let listener -const clearWatchOrientation = () => { - if (watchOrientationId) { - plus.orientation.clearWatch(watchOrientationId) - watchOrientationId = false - } -} +const callbackIds = [] -export function enableCompass ({ - enable -}) { - if (enable) { - clearWatchOrientation() - watchOrientationId = plus.orientation.watchOrientation((o) => { - publish('onCompassChange', { - direction: o.magneticHeading, - errMsg: 'enableCompass:ok' +export function startCompass (options, callbackId) { + listener = listener || plus.orientation.watchOrientation((res) => { + callbackIds.forEach(callbackId => { + invoke(callbackId, { + direction: res.magneticHeading }) - }, (e) => { - publish('onCompassChange', { - errMsg: 'enableCompass:fail' - }) - }, { - frequency: DEVICE_FREQUENCY }) - if (!isWatchOrientation) { - isWatchOrientation = true - const webview = getLastWebview() - if (webview) { - webview.addEventListener('close', () => { - plus.orientation.clearWatch(watchOrientationId) - }) - } + }, err => { + listener = null + invoke(callbackId, { + errMsg: `startCompass:fail ${err.message}` + }) + }, { + frequency: DEVICE_FREQUENCY + }) + setTimeout(() => { + invoke(callbackId, { + errMsg: 'startCompass:ok' + }) + }, DEVICE_FREQUENCY) +} + +export function stopCompass () { + if (listener) { + plus.orientation.clearWatch(listener) + listener = null + } + return {} +} + +export function onCompassChange (callbackId) { + if (!callbackIds.length) { + startCompass() + } + callbackIds.push(callbackId) +} + +export function offCompassChange (callbackId) { + // 暂不支持移除所有监听 + if (callbackId) { + const index = callbackIds.indexOf(callbackId) + if (index >= 0) { + callbackIds.splice(index, 1) } - } else { - clearWatchOrientation() } - return { - errMsg: 'enableCompass:ok' + if (!callbackIds.length) { + stopCompass() } } diff --git a/src/platforms/h5/service/api/device/compass.js b/src/platforms/h5/service/api/device/compass.js index a40aea678fd4f660becba03797e0f80e471bb456..2905de0c241fa6173172dd92aa4b6fc11da1d044 100644 --- a/src/platforms/h5/service/api/device/compass.js +++ b/src/platforms/h5/service/api/device/compass.js @@ -1,41 +1,52 @@ -const callbacks = [] -var listener -/** - * 监听罗盘数据 - * @param {*} callbackId - */ -export function onCompassChange (callbackId) { - callbacks.push(callbackId) - if (!listener) { - startCompass() - } -} -/** - * 开始监听罗盘数据 - */ -export function startCompass () { +let listener + +const callbackIds = [] + +export function startCompass (options, callbackId) { const { invokeCallbackHandler: invoke } = UniServiceJSBridge - if (window.DeviceOrientationEvent) { + if (!window.DeviceOrientationEvent) { + return { + errMsg: 'startCompass:fail' + } + } + function addEventListener () { listener = function (event) { - var direction = 360 - event.alpha - callbacks.forEach(callbackId => { + const direction = 360 - event.alpha + callbackIds.forEach(callbackId => { invoke(callbackId, { - errMsg: 'onCompassChange:ok', direction: direction || 0 }) }) } window.addEventListener('deviceorientation', listener, false) - return {} - } else { - throw new Error('device nonsupport deviceorientation') } + if (!listener) { + if (DeviceOrientationEvent.requestPermission) { + DeviceOrientationEvent.requestPermission().then((res) => { + if (res === 'granted') { + addEventListener() + invoke(callbackId, { + errMsg: 'startCompass:ok' + }) + } else { + invoke(callbackId, { + errMsg: `startCompass:fail ${res}` + }) + } + }).catch(error => { + invoke(callbackId, { + errMsg: `startCompass:fail ${error}` + }) + }) + return + } + addEventListener() + } + return {} } -/** - * 停止监听罗盘数据 - */ + export function stopCompass () { if (listener) { window.removeEventListener('deviceorientation', listener, false) @@ -43,3 +54,23 @@ export function stopCompass () { } return {} } + +export function onCompassChange (callbackId) { + if (!callbackIds.length) { + startCompass() + } + callbackIds.push(callbackId) +} + +export function offCompassChange (callbackId) { + // 暂不支持移除所有监听 + if (callbackId) { + const index = callbackIds.indexOf(callbackId) + if (index >= 0) { + callbackIds.splice(index, 1) + } + } + if (!callbackIds.length) { + stopCompass() + } +} diff --git a/src/platforms/h5/view/components/map/index.vue b/src/platforms/h5/view/components/map/index.vue index 73f681d97de09bc6a2c6f43fff264d342df5d989..461d08e5dbf3a56dadd03765462785724d36e54a 100644 --- a/src/platforms/h5/view/components/map/index.vue +++ b/src/platforms/h5/view/components/map/index.vue @@ -1,11 +1,11 @@