Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute device orientation

I'm trying to read absolute DeviceOrientation event values to create HTML5 compass Mobile Web App.

If I'm using this code, I can get alpha, beta and gamma values without a problem:

window.addEventListener('deviceorientation', function(event) {
          alpha = event.alpha;
          beta = event.beta;
          gamma = event.gamma;
  });

However, those values aren't helpful to me because I need to get absolute values to find exact position of North etc. I've found this article which explains how to use absolute values of event.

When I change code to this:

window.addEventListener('deviceorientationabsolute', function(event) {
    alpha = event.alpha;
    beta = event.beta;
    gamma = event.gamma;
    var absolute = event.absolute;
});

Alpha, beta and gamma values are set to null. This is strange to me, because event.absolute value is set to true. According to that, event values should be displayed.

Anyone has or had any similar problems? Any help would be appreciated; also if there is some working example of HTML5 Compass App it would be helpful to post it here.

Thank you in advance!

like image 933
Coelacanth Avatar asked Apr 10 '18 11:04

Coelacanth


2 Answers

Try to force AddEventListenerOptions value to true, like this:

deviceOrientationEvent: function(event){
    console.log(event.alpha);
};
window.addEventListener("deviceorientationabsolute", deviceOrientationEvent, true);
like image 87
Lorenzo Grossi Avatar answered Sep 30 '22 19:09

Lorenzo Grossi


Try this snippet:

deviceOrientationEvent: function(event){
  console.log(event.alpha);
};

window.addEventListener("deviceorientationabsolute", deviceOrientationEvent, true);
like image 26
Saifullah Avatar answered Sep 30 '22 17:09

Saifullah