| commit | 124ec5540ed6fe284f65665546420ae6d35beace | [log] [tgz] |
|---|---|---|
| author | Steve Gill <stevengill97@gmail.com> | Wed Jun 17 17:35:35 2015 -0700 |
| committer | Steve Gill <stevengill97@gmail.com> | Wed Jun 17 17:35:35 2015 -0700 |
| tree | ddb9f8a0175df784533ddaa568f5f6421196146b | |
| parent | 427d49f61297f749b09b971c00dfeedc289c1809 [diff] |
CB-9192 Incremented plugin version.
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);
}
cordova plugin add cordova-plugin-device-orientation
Get the current compass heading. The compass heading is returned via a CompassHeading object using the compassSuccess callback function.
navigator.compass.getCurrentHeading(compassSuccess, compassError);
function onSuccess(heading) {
alert('Heading: ' + heading.magneticHeading);
};
function onError(error) {
alert('CompassError: ' + error.code);
};
navigator.compass.getCurrentHeading(onSuccess, onError);
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:
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);
Values for current heading are randomly generated in order to simulate the compass.
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.
filter is not supported.filter.filter.filter.filter.Stop watching the compass referenced by the watch ID parameter.
navigator.compass.clearWatch(watchID);
navigator.compass.watchHeading.var watchID = navigator.compass.watchHeading(onSuccess, onError, options); // ... later on ... navigator.compass.clearWatch(watchID);
A CompassHeading object is returned to the compassSuccess callback function.
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)
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
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.
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.
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.
A CompassError object is returned to the compassError callback function when an error occurs.
CompassError.COMPASS_INTERNAL_ERRCompassError.COMPASS_NOT_SUPPORTED