问题
I am developping an hybrid App using Cordova on Android platform. I would like to get the compass heading between my device orientation and the true North.
However the 'deviceorientationabsolute' event (as documented here https://developers.google.com/web/updates/2016/03/device-orientation-changes) currently gives me the compass value relative to the initial orientation of my device. I was expecting an absolute value in reference to the true North.
Using chrome console, I debugged my device and checked that the 'deviceorientationabsolute' event is available
('ondeviceorientationabsolute' in window)
true
I also asked what version of navigator is being used for the webview:
navigator.userAgent
"Mozilla/5.0 (Linux; Android 8.1.0; Android SDK built for x86 Build/OSM1.180201.031; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36"
Here is for Cordova version (in project folder)
cordova --version
9.0.0 (cordova-lib@9.0.1)
Here is the code I am using to get the compass heading
window.addEventListener("deviceorientationabsolute", getorientation, true);
function getorientation(event) {
compassbearing = Number(360 - event.alpha).toFixed(1);
$("#InputCompassbearing").val(compassbearing);
}
回答1:
This behaviour was apparently caused by a problem of compass calibration on my device. After calibrating the compass, it worked fine.
What troubles me is that the event "needs calibration" was not fired. It gave me the compass relative value instead which is probably a misbehaviour from the Chrome or Android API.
来源:https://stackoverflow.com/questions/57377456/how-to-get-absolute-compass-heading-on-cordova-android-platform