← Plugins / Compass
This Apache Cordova plugin reads the device's compass — the direction it points relative to magnetic or true north, measured in degrees. Use it for navigation, augmented-reality overlays and any feature that needs to know which way the user is facing.
cordova plugin add cordova-plugin-device-orientation
The plugin lives on navigator.compass. Call these after deviceready:
| Method | Description |
|---|---|
getCurrentHeading(success, error) | Read the compass heading once. |
watchHeading(success, error, options) | Read the heading repeatedly; returns a watch ID. options.frequency sets the interval; options.filter reports only changes above N degrees. |
clearWatch(watchID) | Stop a watch started with watchHeading. |
| Property | Description |
|---|---|
magneticHeading | Heading in degrees (0–359.99) relative to magnetic north. |
trueHeading | Heading relative to true (geographic) north, where available. |
headingAccuracy | The deviation in degrees between reported and actual heading. |
timestamp | When the reading was taken, in milliseconds. |
Watch the heading and rotate a compass needle to match:
var watchID = navigator.compass.watchHeading(
function (heading) {
var deg = heading.magneticHeading;
document.getElementById("needle").style.transform =
"rotate(" + deg + "deg)";
},
function () {
console.error("No compass");
},
{ frequency: 100, filter: 2 }
);
filter option to fire only on meaningful changes and save battery.trueHeading needs location services; fall back to magneticHeading when it's unavailable.headingAccuracy if precision matters.