完善地图交互、动画与罗盘调试
This commit is contained in:
@@ -52,6 +52,44 @@ function interpolateHeadingDeg(currentDeg: number, targetDeg: number, factor: nu
|
||||
return normalizeHeadingDeg(currentDeg + normalizeHeadingDeltaDeg(targetDeg - currentDeg) * factor)
|
||||
}
|
||||
|
||||
function resolveMotionCompassHeadingDeg(
|
||||
alpha: number | null,
|
||||
beta: number | null,
|
||||
gamma: number | null,
|
||||
): number | null {
|
||||
if (alpha === null) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (beta === null || gamma === null) {
|
||||
return normalizeHeadingDeg(360 - alpha)
|
||||
}
|
||||
|
||||
const alphaRad = alpha * Math.PI / 180
|
||||
const betaRad = beta * Math.PI / 180
|
||||
const gammaRad = gamma * Math.PI / 180
|
||||
|
||||
const cA = Math.cos(alphaRad)
|
||||
const sA = Math.sin(alphaRad)
|
||||
const sB = Math.sin(betaRad)
|
||||
const cG = Math.cos(gammaRad)
|
||||
const sG = Math.sin(gammaRad)
|
||||
|
||||
const headingX = -cA * sG - sA * sB * cG
|
||||
const headingY = -sA * sG + cA * sB * cG
|
||||
|
||||
if (Math.abs(headingX) < 1e-6 && Math.abs(headingY) < 1e-6) {
|
||||
return normalizeHeadingDeg(360 - alpha)
|
||||
}
|
||||
|
||||
let headingRad = Math.atan2(headingX, headingY)
|
||||
if (headingRad < 0) {
|
||||
headingRad += Math.PI * 2
|
||||
}
|
||||
|
||||
return normalizeHeadingDeg(headingRad * 180 / Math.PI)
|
||||
}
|
||||
|
||||
function getApproxDistanceMeters(
|
||||
a: { lon: number; lat: number },
|
||||
b: { lon: number; lat: number },
|
||||
@@ -530,13 +568,13 @@ export class TelemetryRuntime {
|
||||
}
|
||||
|
||||
if (event.type === 'device_motion_updated') {
|
||||
const nextDeviceHeadingDeg = event.alpha === null
|
||||
const motionHeadingDeg = resolveMotionCompassHeadingDeg(event.alpha, event.beta, event.gamma)
|
||||
const nextDeviceHeadingDeg = motionHeadingDeg === null
|
||||
? this.state.deviceHeadingDeg
|
||||
: (() => {
|
||||
const nextHeadingDeg = normalizeHeadingDeg(360 - event.alpha * 180 / Math.PI)
|
||||
return this.state.deviceHeadingDeg === null
|
||||
? nextHeadingDeg
|
||||
: interpolateHeadingDeg(this.state.deviceHeadingDeg, nextHeadingDeg, DEVICE_HEADING_SMOOTHING_ALPHA)
|
||||
? motionHeadingDeg
|
||||
: interpolateHeadingDeg(this.state.deviceHeadingDeg, motionHeadingDeg, DEVICE_HEADING_SMOOTHING_ALPHA)
|
||||
})()
|
||||
|
||||
this.state = {
|
||||
|
||||
Reference in New Issue
Block a user