Skip to content

Commit

Permalink
PB-170: Add debug statements and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJoss committed Jul 16, 2024
1 parent 127ca60 commit e98c8fc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { View } from 'ol'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { computed, onBeforeUnmount, onMounted, watch } from 'vue'
import { useStore } from 'vuex'

import { IS_TESTING_WITH_CYPRESS, VIEW_MIN_RESOLUTION } from '@/config'
Expand All @@ -16,8 +16,8 @@ if (IS_TESTING_WITH_CYPRESS) {
}

export default function useViewBasedOnProjection(map) {
const deviceOrientation = ref(0)
const circularAverage = ref(0)
let deviceOrientation = 0
let circularAverage = 0
let count = 0
let sumSin = 0
let sumCos = 0
Expand Down Expand Up @@ -96,31 +96,32 @@ export default function useViewBasedOnProjection(map) {
})

const handleOrientation = function (event) {
deviceOrientation.value = round((event.alpha / 180) * Math.PI, 2)
if (store.state.position.headingIsAbsolute != event.absolute) {
log.debug('Absolutism of device heading is set to ', event.absolute)
store.dispatch('setHeadingIsAbsolute', event.absolute)
}
deviceOrientation = round((event.alpha / 180) * Math.PI, 2)
count = count + 1
sumSin = sumSin + Math.sin(deviceOrientation.value)
sumCos = sumCos + Math.cos(deviceOrientation.value)
}

const checkIfOrientationIsAbsolute = function (event) {
store.dispatch('setHeadingIsAbsolute', event.absolute)
window.removeEventListener('deviceorientation', checkIfOrientationIsAbsolute)
sumSin = sumSin + Math.sin(deviceOrientation)
sumCos = sumCos + Math.cos(deviceOrientation)
if (count === 1) {
console.debug('Read device orientation alpha value: ', event.alpha)
}
}

function toggleAutoRotateListener() {
if (geolocationIsActive.value) {
// request permissions if IOS
if (typeof DeviceMotionEvent.requestPermission === 'function') {
DeviceMotionEvent.requestPermission().then(() => {
window.addEventListener('deviceorientation', checkIfOrientationIsAbsolute)
window.addEventListener('deviceorientation', handleOrientation)
})
} else {
window.addEventListener('deviceorientation', checkIfOrientationIsAbsolute)
window.addEventListener('deviceorientation', handleOrientation)
}
} else {
if (!store.state.position.resetRotation && autoRotation.value) {
store.dispatch('setRotation', deviceOrientation.value)
store.dispatch('setRotation', deviceOrientation)
}
window.removeEventListener('deviceorientation', handleOrientation)
}
Expand All @@ -137,13 +138,13 @@ export default function useViewBasedOnProjection(map) {
const newCircularAverage = Math.atan2(sumSin, sumCos)
const certaintyThreshold = (sumSin / count) ** 2 + (sumCos / count) ** 2 > 0.9
const deltaThreshold =
Math.abs(circularAverage.value - newCircularAverage) > (2 * Math.PI) / 180
Math.abs(circularAverage - newCircularAverage) > (2 * Math.PI) / 180
if (autoRotation.value && certaintyThreshold && deltaThreshold) {
viewsForProjection[projection.value.epsg].animate({
rotation: newCircularAverage,
duration: animationDuration,
})
circularAverage.value = newCircularAverage
circularAverage = newCircularAverage
}
if (geolocationIsActive.value) {
store.dispatch('setHeading', newCircularAverage)
Expand Down
3 changes: 1 addition & 2 deletions src/modules/map/components/toolbox/MapToolbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* By default the toolbox only contains the zoom in/out buttons
*/
import { computed, onMounted, toRefs } from 'vue'
import { computed, toRefs } from 'vue'
import { useStore } from 'vuex'
import OpenLayersCompassButton from '@/modules/map/components/openlayers/OpenLayersCompassButton.vue'
Expand Down Expand Up @@ -37,7 +37,6 @@ const isFullscreenMode = computed(() => store.state.ui.fullscreenMode)
const hasDevSiteWarning = computed(() => store.getters.hasDevSiteWarning)
const isDrawingMode = computed(() => store.state.drawing.drawingOverlay.show)
const is3dActive = computed(() => store.state.cesium.active)
</script>

<template>
Expand Down
1 change: 0 additions & 1 deletion src/store/modules/position.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const state = {
*/
rotation: 0,


/**
* The heading reported by the device
*
Expand Down

0 comments on commit e98c8fc

Please sign in to comment.