Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
To query the device’s orientation, simply call the following method:
navigator.compass.getCurrentHeading(successFunction,
errorFunction);
Passed to the API are the names of two functions that are called depending on whether the API is returning a result. The successFunction is called when a reading has been successfully made, and the errorFunction is called when there is an error reading the compass.
When called, the successFunction is passed the compassHeading object, which consists of the following components:
• magneticHeading: The device’s current heading in degrees ranging from 0 to 359.99.
• trueHeading: The device’s current heading relative to the geographic North Pole in degrees ranging from 0 to 359.99. A negative value indicates that a value could not be determined.