5

I am working on a mobile web project that needs to know the compass direction the user's device is pointing. It's incredibly simple right now, but here's what I have:

        var updateDirection = function (evt) {
            $("#direction").val(evt.alpha);
        };

        window.addEventListener("deviceorientation", updateDirection);

Based on what I've researched so far, the alpha channel of the event should be the compass position. However, I've observed (and read) that there are a wide variety of implementations based on OS and browser.

  • On my HTC smartphone, in Chrome or the default Android browser, I only get a reasonable reading (0 degrees = North, 90 = East, so on) when I hold the phone perfectly vertical in a "selfie" position. Any angle to the phone throws readings quite far off.
  • On my Surface Pro using Chrome, I can't get a reading greater than about 50.
  • On my Surface Pro using Edge, I get very reasonable readings, but only when I hold the device horizontal, as if it was laying on a table.

It seems likely that people have achieved getting the compass direction in a mobile browser regardless of device. Is there some library I can use to do this? Or is it necessary to simply code for many different specific scenarios, like this example, which also didn't work for all the devices listed:

Device Orientation Events

SouthShoreAK
  • 4,176
  • 2
  • 26
  • 48
  • 1
    https://mobiforge.com/design-development/html5-mobile-web-device-orientation-events it is not good news to you. :( – lv0gun9 Aug 30 '15 at 00:22
  • 1
    For something completely different you could try using [geolocation](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation) and determine a heading vector from two consecutive lon/lat coordinates. Then you could use something like [this](http://stackoverflow.com/a/22740092/2592445) to calculate a compass reading. – Fasermaler Aug 31 '15 at 19:48

1 Answers1

-2

Is that really necessary for you to use javascript to find out orientation?

You could possibly achieve the same result with CSS media queries

Vlad Miller
  • 2,255
  • 1
  • 20
  • 38
  • I'm not so concerned with device orientation in terms of vertical or horizontal. I'm looking for the compass heading the device is pointing - can css media queries do that? – SouthShoreAK Aug 30 '15 at 20:09
  • Unfortunately I don't think it's possible. Sorry I thought you worried about device orientation so you can adjust interface – Vlad Miller Aug 30 '15 at 21:04