Obtains the current orientation of the device.
Method:
Compass. getCurrentHeading
Compass. watchHeading
Compass. clearWatch
Parameters:
CompassSuccess
CompassError
CompassOptions
Compass. getCurrentHeading
Obtains the current orientation of the compass.
Simple Example:
Avigator. compass. getCurrentHeading (compassSuccess, compassError, compassOptions );
Copy Code Description: The compass is a sensor that detects the direction or orientation of a device. The measurement unit is degree. The value ranges from 0 degrees to 359.99 degrees.
The compassSuccess callback function is used to return the orientation data of the compass.
Supported platforms:
Android
IPhone
Simple Example:
Function onSuccess (heading) {alert ('heading: '+ Heading);} function onError () {alert ('onerror! ');} Navigator. compass. getCurrentHeading (onSuccess, onError );
Copy the complete sample code:
<! DOCTYPE html>
// Wait for loading PhoneGap
Document. addEventListener ("deviceready", onDeviceReady, false );
// PhoneGap loaded
Function onDeviceReady (){
Navigator. compass. getCurrentHeading (onSuccess, onError );
}
// OnSuccess: returns the current orientation data
Function onSuccess (heading ){
Alert ('heading: '+ Heading );
}
// OnError: An error occurred while returning the oriented data.
Function onError (){
Alert ('onerror! ');
}
</SCRIPT>
</Head>
<Body>
<H1> Example
<P> getCurrentHeading </p>
</Body>
</Html>
Copy the code compass. watchHeading to get the angle of the compass orientation at a fixed interval.
Var watchID = navigator. compass. watchHeading (compassSuccess, compassError, [compassOptions]);
Copy Code Description: The compass is a sensor that detects the direction or orientation of a device. The measurement unit is degree. The value ranges from 0 degrees to 359.99 degrees.
Compass. watchHeading obtains the current orientation of the device at a fixed time. After each orientation is obtained, the headingSuccess callback function is executed. The frequency parameter of the compassOptions object can be used to set the interval in milliseconds.
The returned watch ID is a reference of the compass monitoring cycle. You can call this watch ID through compass. clearWatch to stop monitoring the compass.
Supported platforms:
Android
IPhone
Simple Example:
Function onSuccess (heading ){
Var element = document. getElementById ('heading ');
Element. innerHTML = 'heading: '+ Heading;
}
Function onError (){
Alert ('onerror! ');
}
Var options = {frequency: 3000}; // update every 3 seconds
Var watchID = navigator. compass. watchHeading (onSuccess, onError, options );
Copy the complete sample code: <! DOCTYPE html>
<Html>
<Head>
<Title> Compass Example </title>
<Script type = "text/javascript" charset = "UTF-8" src = "phonegap. js"> </script>
<Script type = "text/javascript" charset = "UTF-8">
// WatchID is the reference of the current "watchHeading"
Var watchID = null;
// Wait for loading PhoneGap
Document. addEventListener ("deviceready", onDeviceReady, false );
// PhoneGap loaded
Function onDeviceReady (){
StartWatch ();
}
// Start to monitor the compass
Function startWatch (){
// Update the orientation of the compass every 3 seconds.
Var options = {frequency: 3000 };
WatchID = navigator. compass. watchHeading (onSuccess, onError, options );
}
// Stop monitoring the compass
Function stopWatch (){
If (watchID ){
Navigator. compass. clearWatch (watchID );
WatchID = null;
}
}
// OnSuccess: returns the current orientation of the compass.
Function onSuccess (heading ){
Var element = document. getElementById ('heading ');
Element. innerHTML = 'heading: '+ Heading;
}
// OnError: An error occurred while obtaining the orientation of the compass.
Function onError (){
Alert ('onerror! ');
}
</Script>
</Head>
<Body>
<Div id = "heading"> Waiting for heading... </div>
<Button onclick = "startWatch ();"> Start Watching </button>
<Button onclick = "stopWatch ();"> Stop Watching </button>
</Body>
</Html>
Copy the code compass. clearWatch to stop the compass monitoring pointed to by the watch ID parameter.
Navigator. compass. clearWatch (watchID );
Copy code
WatchID: The reference ID returned by compass. watchHeading.
Supported platforms:
Android
IPhone
Simple Example:
Var watchID = navigator. compass. watchHeading (onSuccess, onError, options );
//... Proceed...
Navigator. compass. clearWatch (watchID );
Copy the complete sample code:
<! DOCTYPE html>
<Html>
<Head>
<Title> Compass Example </title>
<Script type = "text/javascript" charset = "UTF-8" src = "phonegap. js"> </script>
<Script type = "text/javascript" charset = "UTF-8">
// WatchID is the reference of the current "watchHeading"
Var watchID = null;
// Wait for loading PhoneGap
Document. addEventListener ("deviceready", onDeviceReady, false );
// PhoneGap loaded
Function onDeviceReady (){
StartWatch ();
}
// Start to monitor the compass
Function startWatch (){
// Update the compass data every three seconds.
Var options = {frequency: 3000 };
WatchID = navigator. compass. watchHeading (onSuccess, onError, options );
}
// Stop monitoring the compass
Function stopWatch (){
If (watchID ){
Navigator. compass. clearWatch (watchID );
WatchID = null;
}
}
// OnSuccess: returns the current orientation of the compass.
Function onSuccess (heading ){
Var element = document. getElementById ('heading ');
Element. innerHTML = 'heading: '+ Heading;
}
// OnError: An error occurred while obtaining the orientation of the compass.
Function onError (){
Alert ('onerror! ');
}
</Script>
</Head>
<Body>
<Div id = "heading"> Waiting for heading... </div>
<Button onclick = "startWatch ();"> Start Watching </button>
<Button onclick = "stopWatch ();"> Stop Watching </button>
</Body>
</Html>
Copy the code compassSuccess to provide the onSuccess callback function of the compass orientation information.
Function (heading) {// Further Processing
}
Copy code parameters:
Heading: The Orientation Angle of the compass at a certain time point (from 0 degrees to 359.99 degrees ). (Number type)
Example:
Function onSuccess (heading) {alert ('heading: '+ Heading );}
Copy the onError callback function of the compassError method of the Code.
Function () {// handle errors
}
Copy code compassOptions
Optional parameters for customizing the orientation of the search compass.
Option:
Frequency: the number of milliseconds to obtain the orientation of the compass. (Number Type) (default value: 100)
Author: LuoXianXion