admin管理员组文章数量:1122826
I'm trying to get the correct orientation in my web application. On iOS, event.webkitCompassHeading
works very well, but on Android, it is either undefined or returns NaN.
After searching on Google, I found this type of code:
headingMio = Math.round(roundedAlpha + 85) % 360;
if (headingMio < 0) {
headingMio += 360;
}
But it doesn't work in landscape mode. How can I fix this?There is my code:
let lastHeading = null;
let threshold = 5;
let threshold2 = 15;
document.addEventListener('click', async function requestOrientationPermission() {
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
try {
const permissionState = await DeviceOrientationEvent.requestPermission();
if (permissionState === 'granted') {
console.log("Permesso concesso per l'orientamento del dispositivo.");
// Aggiungi l'event listener per l'orientamento
window.addEventListener('deviceorientation', handleDeviceOrientation);
document.removeEventListener('click', requestOrientationPermission);
} else {
console.log("Permesso negato per l'orientamento del dispositivo.");
}
} catch (error) {
console.error('Errore nel richiedere il permesso:', error);
}
} else {
window.addEventListener('deviceorientation', handleDeviceOrientation);
}
});
// Funzione che gestisce i dati di orientamento del dispositivo
function handleDeviceOrientation(event) {
roundedAlpha = Math.round(360 - event.alpha);
roundedBeta = Math.round(event.beta);
roundedGamma = Math.round(event.gamma);
headingMio = Math.round(event.webkitCompassHeading);
if (headingMio === undefined || isNaN(headingMio)) {
if (roundedBeta > 80 && roundedBeta < 105) {
//not worikng landscape
} else {
headingMio = Math.round(roundedAlpha + 85) % 360;
if (headingMio < 0) {
headingMio += 360;
}
}
}
const c = document.getElementById("orientation");
if (lastHeading === null || Math.abs(headingMio - lastHeading) > threshold) {
lastHeading = headingMio;
c.innerText = `Alpha (Z): ${roundedAlpha}, Beta (X - Inclinazione avanti/indietro): ${roundedBeta}, Gamma (Y - Inclinazione laterale): ${roundedGamma}, Nord Magnetico (Compass Heading): ${headingMio}`;
if (!isNaN(headingMio)) {
map.setHeading(parseInt(headingMio, 10));
}
}
c.innerText = `Alpha (Z): ${roundedAlpha}, Beta (X - Inclinazione avanti/indietro): ${roundedBeta}, Gamma (Y - Inclinazione laterale): ${roundedGamma}, Nord Magnetico (Compass Heading): ${headingMio}`;
}
I'm trying to get the correct orientation in my web application. On iOS, event.webkitCompassHeading
works very well, but on Android, it is either undefined or returns NaN.
After searching on Google, I found this type of code:
headingMio = Math.round(roundedAlpha + 85) % 360;
if (headingMio < 0) {
headingMio += 360;
}
But it doesn't work in landscape mode. How can I fix this?There is my code:
let lastHeading = null;
let threshold = 5;
let threshold2 = 15;
document.addEventListener('click', async function requestOrientationPermission() {
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
try {
const permissionState = await DeviceOrientationEvent.requestPermission();
if (permissionState === 'granted') {
console.log("Permesso concesso per l'orientamento del dispositivo.");
// Aggiungi l'event listener per l'orientamento
window.addEventListener('deviceorientation', handleDeviceOrientation);
document.removeEventListener('click', requestOrientationPermission);
} else {
console.log("Permesso negato per l'orientamento del dispositivo.");
}
} catch (error) {
console.error('Errore nel richiedere il permesso:', error);
}
} else {
window.addEventListener('deviceorientation', handleDeviceOrientation);
}
});
// Funzione che gestisce i dati di orientamento del dispositivo
function handleDeviceOrientation(event) {
roundedAlpha = Math.round(360 - event.alpha);
roundedBeta = Math.round(event.beta);
roundedGamma = Math.round(event.gamma);
headingMio = Math.round(event.webkitCompassHeading);
if (headingMio === undefined || isNaN(headingMio)) {
if (roundedBeta > 80 && roundedBeta < 105) {
//not worikng landscape
} else {
headingMio = Math.round(roundedAlpha + 85) % 360;
if (headingMio < 0) {
headingMio += 360;
}
}
}
const c = document.getElementById("orientation");
if (lastHeading === null || Math.abs(headingMio - lastHeading) > threshold) {
lastHeading = headingMio;
c.innerText = `Alpha (Z): ${roundedAlpha}, Beta (X - Inclinazione avanti/indietro): ${roundedBeta}, Gamma (Y - Inclinazione laterale): ${roundedGamma}, Nord Magnetico (Compass Heading): ${headingMio}`;
if (!isNaN(headingMio)) {
map.setHeading(parseInt(headingMio, 10));
}
}
c.innerText = `Alpha (Z): ${roundedAlpha}, Beta (X - Inclinazione avanti/indietro): ${roundedBeta}, Gamma (Y - Inclinazione laterale): ${roundedGamma}, Nord Magnetico (Compass Heading): ${headingMio}`;
}
Share
Improve this question
asked Nov 22, 2024 at 11:00
Pierpaolo LopezPierpaolo Lopez
91 bronze badge
0
1 Answer
Reset to default 0This is a modified version of your code which includes landscape mode handling:
let lastHeading = null;
let threshold = 5;
document.addEventListener('click', async function requestOrientationPermission() {
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
try {
const permissionState = await DeviceOrientationEvent.requestPermission();
if (permissionState === 'granted') {
console.log("Permission granted for device orientation.");
window.addEventListener('deviceorientation', handleDeviceOrientation);
document.removeEventListener('click', requestOrientationPermission);
} else {
console.log("Permission denied for device orientation.");
}
} catch (error) {
console.error('Error requesting permission:', error);
}
} else {
window.addEventListener('deviceorientation', handleDeviceOrientation);
}
});
// Function to handle device orientation data
function handleDeviceOrientation(event) {
const roundedAlpha = Math.round(event.alpha);
const roundedBeta = Math.round(event.beta);
const roundedGamma = Math.round(event.gamma);
let headingMio;
// Check if webkitCompassHeading is available
if (event.webkitCompassHeading !== undefined) {
headingMio = Math.round(event.webkitCompassHeading);
} else {
// Handle the case when webkitCompassHeading is not available
headingMio = (roundedAlpha + (window.orientation || 0) + 360) % 360;
}
const c = document.getElementById("orientation");
// Update the displayed orientation values
if (lastHeading === null || Math.abs(headingMio - lastHeading) > threshold) {
lastHeading = headingMio;
c.innerText = `Alpha (Z): ${roundedAlpha}, Beta (X): ${roundedBeta}, Gamma (Y): ${roundedGamma}, Magnetic North (Compass Heading): ${headingMio}`;
if (!isNaN(headingMio)) {
map.setHeading(parseInt(headingMio, 10));
}
}
}
The following code has been changed as mentioned here.
- When webkitCompassHeading is not available, the code calculates headingMio using roundedAlpha adjusted for the current window orientation (window.orientation), which provides a more accurate sense of direction based on the device's current orientation.
- The calculation (roundedAlpha + (window.orientation || 0) + 360)% 360 makes the heading more accurate in landscape view by adjusting it based on the device's orientation.
本文标签: javascriptIncorrect orientation on Android in landscape modeStack Overflow
版权声明:本文标题:javascript - Incorrect orientation on Android in landscape mode - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304202a1932151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论