DedrisFrameworkDedrisFramework

← Plugins  /  Compass

🧭

Compass

cordova-plugin-device-orientation
iOS Android

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.

Installation

terminal
cordova plugin add cordova-plugin-device-orientation

API

The plugin lives on navigator.compass. Call these after deviceready:

MethodDescription
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.

The CompassHeading object

PropertyDescription
magneticHeadingHeading in degrees (0–359.99) relative to magnetic north.
trueHeadingHeading relative to true (geographic) north, where available.
headingAccuracyThe deviation in degrees between reported and actual heading.
timestampWhen the reading was taken, in milliseconds.

Usage example

Watch the heading and rotate a compass needle to match:

www/js/app.js
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 }
);

Tips

📚 Source: the technical reference on this page (the methods and the CompassHeading object) is based on the official Apache Cordova device-orientation documentation. Descriptions and examples have been rewritten in DedrisFramework's own words. Apache Cordova documentation is published by the Apache Software Foundation under the Apache License 2.0.
← Back to plugins Start a project