How to get absolute compass heading on Cordova Android platform

↘锁芯ラ 提交于 2021-01-29 06:01:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!