web-applicationsmobilecompass-geolocation

Access device compass from web app with Javascript


Is it possible to access the compass with Javascript on an iphone/ android device from a web app? Have been looking all over the net for hours I know you can access the accelerometer with

window.ondevicemotion = function(event)

Does anyone know if you can access the information from the compass??


Solution

  • On iPhone, you can get the compass value like this.

    window.addEventListener('deviceorientation', function(e) {
        console.log(e.webkitCompassHeading);
    }, false);
    

    Hope this help.