CB-9426 Fix exception when using device orientation plugin on browser platform.

The plugin's plugin.xml defines a general <js-module> with the name 'compass'. It defines another <js-module> for the browser platform (that merges with the general module) with the same name. Modules with the same name is not allowed, so Cordova throws an exception at runtime when trying to define the browser version of the module.

Since the only difference in the browser version of the module is to fake up deviceorientation events, I've deleted the browser version and added that logic to the general module, with the following change: rather than starting a new event timer everytime watchHeading() is called (which could happen multiple times resulting in multiple superfluous timers), we have a single event timer going as long as there are active watchers.
3 files changed
tree: 5378ac4c33b7892627491cd0b180083a248c23bd
  1. doc/
  2. src/
  3. tests/
  4. www/
  5. .gitignore
  6. CONTRIBUTING.md
  7. LICENSE
  8. NOTICE
  9. package.json
  10. plugin.xml
  11. README.md
  12. RELEASENOTES.md
README.md

cordova-plugin-device-orientation

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);
}

Installation

cordova plugin add cordova-plugin-device-orientation

Supported Platforms

  • Amazon Fire OS
  • Android
  • BlackBerry 10
  • Browser
  • Firefox OS
  • iOS
  • Tizen
  • Windows Phone 7 and 8 (if available in hardware)
  • Windows 8

Methods

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

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:

  • frequency: How often to retrieve the compass heading in milliseconds. (Number) (Default: 100)
  • filter: The change in degrees required to initiate a watchHeading success callback. When this value is set, frequency is ignored. (Number)

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.

Amazon Fire OS Quirks

  • filter is not supported.

Android Quirks

  • No support for filter.

Firefox OS Quirks

  • No support for filter.

Tizen Quirks

  • No support for filter.

Windows Phone 7 and 8 Quirks

  • No support for filter.

navigator.compass.clearWatch

Stop watching the compass referenced by the watch ID parameter.

navigator.compass.clearWatch(watchID);
  • watchID: The ID returned by navigator.compass.watchHeading.

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

  • magneticHeading: The heading in degrees from 0-359.99 at a single moment in time. (Number)

  • trueHeading: The heading relative to the geographic North Pole in degrees 0-359.99 at a single moment in time. A negative value indicates that the true heading can't be determined. (Number)

  • headingAccuracy: The deviation in degrees between the reported heading and the true heading. (Number)

  • timestamp: The time at which this heading was determined. (milliseconds)

Amazon Fire OS Quirks

  • trueHeading is not supported, but reports the same value as magneticHeading

  • headingAccuracy is always 0 because there is no difference between the magneticHeading and trueHeading

Android Quirks

  • The trueHeading property is not supported, but reports the same value as magneticHeading.

  • The headingAccuracy property is always 0 because there is no difference between the magneticHeading and trueHeading.

Firefox OS Quirks

  • The trueHeading property is not supported, but reports the same value as magneticHeading.

  • The headingAccuracy property is always 0 because there is no difference between the magneticHeading and trueHeading.

iOS Quirks

  • The trueHeading property is only returned for location services enabled via navigator.geolocation.watchLocation().

  • For iOS 4 devices and above, heading factors in the device's current orientation, and does not reference its absolute position, for apps that supports that orientation.

CompassError

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

Properties

  • code: One of the predefined error codes listed below.

Constants

  • CompassError.COMPASS_INTERNAL_ERR
  • CompassError.COMPASS_NOT_SUPPORTED