apache / cordova-plugin-device-orientation

Apache Cordova Device Orientation Plugin
https://cordova.apache.org/
Apache License 2.0
58 stars 83 forks source link
android cordova hacktoberfest ios java javascript library mobile nodejs objective-c

title: Device Orientation description: Access compass data.

cordova-plugin-device-orientation

Android Testsuite Chrome Testsuite iOS Testsuite Lint Test

Usage Notice

With the W3C Device Orientation API, Android, iOS, and Windows devices may not need this plugin anymore.

However, on iOS 13+, potential issues with permissions and secure contexts can arise. Therefore it is recommended to use this plugin as it uses a native implementation.


Description

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.

Access is via a global navigator.compass object.

Although the object is attached to the global scoped navigator, it is not available until after the deviceready event.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log(navigator.compass);
}

Report issues on the Apache Cordova issue tracker

Installation

cordova plugin add cordova-plugin-device-orientation

Supported Platforms

Methods

navigator.compass.getCurrentHeading

Get the current compass heading. The compass heading is returned via a CompassHeading object using the compassSuccess callback function.

navigator.compass.getCurrentHeading(compassSuccess, compassError);

Example

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

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

navigator.compass.getCurrentHeading(onSuccess, onError);

navigator.compass.watchHeading

Gets the device's current heading at a regular interval. Each time the heading is retrieved, the headingSuccess callback function is executed.

The returned watch ID references the compass watch interval. The watch ID can be used with navigator.compass.clearWatch to stop watching the navigator.compass.

var watchID = navigator.compass.watchHeading(compassSuccess, compassError, [compassOptions]);

compassOptions may contain the following keys:

Example

function onSuccess(heading) {
    var element = document.getElementById('heading');
    element.innerHTML = 'Heading: ' + heading.magneticHeading;
};

function onError(compassError) {
    alert('Compass error: ' + compassError.code);
};

var options = {
    frequency: 3000
}; // Update every 3 seconds

var watchID = navigator.compass.watchHeading(onSuccess, onError, options);

Browser Quirks

Values for current heading are randomly generated in order to simulate the compass.

iOS Quirks

Only one watchHeading can be in effect at one time in iOS. If a watchHeading uses a filter, calling getCurrentHeading or watchHeading uses the existing filter value to specify heading changes. Watching heading changes with a filter is more efficient than with time intervals.

Android Quirks

navigator.compass.clearWatch

Stop watching the compass referenced by the watch ID parameter.

navigator.compass.clearWatch(watchID);

Example

var watchID = navigator.compass.watchHeading(onSuccess, onError, options);

// ... later on ...

navigator.compass.clearWatch(watchID);

CompassHeading

A CompassHeading object is returned to the compassSuccess callback function.

Properties

Android Quirks

iOS Quirks

CompassError

A CompassError object is returned to the compassError callback function when an error occurs.

Properties

Constants