This plugin provides access to the device’s compass. The compass is a sensor that detects the direction or heading that the device is pointed, typically from the top of the device. It measures the heading in degrees from 0 to 359.99, where 0 is north.

 

Methods

  • navigator.compass.getCurrentHeading
  • navigator.compass.watchHeading
  • navigator.compass.clearWatch

 

Example:

function onSuccess(heading) {
  alert(‘Heading: ‘ + heading.magneticHeading);
};

function onError(error) {
  alert(‘CompassError: ‘ + error.code);
};

navigator.compass.getCurrentHeading(onSuccess, onError);

watchHeading and clearWatch Example

var watchId;

watchHeading = function() {
  var options = {frequency:200};// watch every 0.2 seconds
  watchId = navigator.compass.watchHeading(watchHeadingSuccess, watchHeadingFail, options);
};

clearHeading = function() {
  navigator.compass.clearWatch(watchId);//stop
};

watchHeadingSuccess = function(heading) {
  document.getElementById(“id-of-element-where-value-goes”).innerHTML = “Magnetic Headig: “+heading.magneticHeading;
};

watchHeadingFail = function(error) {
  alert(“Heading fail: “+ error.code);
};

Source Documentation: http://plugins.cordova.io/#/package/org.apache.cordova.device-orientation

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *