The orientation module manages the direction information of the device, including alpha, beta and gamma, through plus Orientation to obtain the device direction management object.
method:
- getCurrentOrientation : obtain the direction information of the current device, including alpha, beta and gamma direction information
- watchOrientation : monitor the change of equipment direction information
- clearWatch : turn off the direction information of the monitoring device
Object:
- OrientationOption : JSON object, monitoring device direction sensor parameters
- Rotation : JSON object, device direction information data
Callback method:
- OrientationSuccessCallback : callback function to obtain device direction information successfully
- OrientationErrorCallback : callback function failed to get device direction information
jurisdiction:
5 + function modules (permissions)
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#ffffff">{</span> <span style="color:#87ceeb">// ...</span> <span style="color:#ffa0a0">"permissions"</span><span style="color:#ffffff">:{</span> <span style="color:#87ceeb">// ...</span> <span style="color:#ffa0a0">"Orientation"</span><span style="color:#ffffff">:</span> <span style="color:#ffffff">{</span> <span style="color:#ffa0a0">"description"</span><span style="color:#ffffff">:</span> <span style="color:#Ffa0a0 ">" direction sensor“</span> <span style="color:#ffffff">}</span> <span style="color:#ffffff">}</span> <span style="color:#ffffff">}</span> </code></span></span>
Obtain the direction information of the current device, including alpha, beta and gamma direction information
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">orientation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentOrientation</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB </span><span style="color:#ffffff">);</span> </code></span></span>
explain:
Direction information refers to the rotation angle of the equipment with the axis of z, x and y respectively relative to the horizontal initial direction, and the corresponding values are alpha, beta and gamma. The direction information can be returned through the successCB callback function. If the direction information acquisition fails, call the callback function errorCB
Parameters:
- successCB: ( OrientationSuccessCallback ) required # callback function for obtaining device direction information successfully
- errorCB: ( OrientationErrorCallback (optional) callback function for failed to obtain device direction information
Return value:
void: None
Platform support:
- Android - 2.2 + (supported):
support
- iOS - 4.3 + (supported):
support
Example:
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#cccccc"><!DOCTYPE html></span> <span style="color:#f0e68c"><strong><html></strong></span> <span style="color:#f0e68c"><strong><head></strong></span> <span style="color:#f0e68c"><strong><meta</strong></span> <span style="color:#bdb76b"><strong>charset</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"utf-8"</span><span style="color:#f0e68c"><strong>></strong></span> <span style="color:#f0e68c"><strong><title></strong></span><span style="color:#ffffff">Orientation Example</span><span style="color:#f0e68c"><strong></title></strong></span> <span style="color:#f0e68c"><strong><script</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"text/javascript"</span><span style="color:#f0e68c"><strong>></strong></span> <span style="color:#87ceeb">// After expanding API, call onPlusReady callback function </span><span style= "color:#ffffff" > after loading. document</span><span style="color:#ffffff">.</span><span style="color:#ffffff">addEventListener</span><span style="color:#ffffff">(</span> <span style="color:#ffa0a0">"plusready"</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>false</strong></span> <span style="color:#ffffff">);</span> <span style="color:#87ceeb "> / / after loading the extension API, you can call the extension API normally</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">orientation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentOrientation</span><span style="color:#ffffff">(</span> <span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#ffffff">(</span><span style="color:#ffffff"> o </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff"> alert</span><span style="color:#ffffff">(</span> <span style="color:#ffa0a0">"Orientation\nAlpha:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> o</span><span style="color:#ffffff">.</span><span style="color:#ffffff">alpha </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\nBeta:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> o</span><span style="color:#ffffff">.</span><span style="color:#ffffff">beta </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\nGamma:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> o</span><span style="color:#ffffff">.</span><span style="color:#ffffff">gamma </span><span style="color:#ffffff">);</span> <span style="color:#ffffff">}</span> <span style="color:#ffffff">);</span> <span style="color:#ffffff">}</span> <span style="color:#f0e68c"><strong></script></strong></span> <span style="color:#f0e68c"><strong></head></strong></span> <span style="color:#f0e68c"><strong><body></strong></span><span style="color:#ffffff"> Orientation </span><span style="color:#f0e68c"><strong></body></strong></span> <span style="color:#f0e68c"><strong></html></strong></span> </code></span></span>
Precautions for using plus in uni app
Monitor the change of equipment direction information
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#98fb98">Number</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">orientation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">watchOrientation</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> option </span><span style="color:#ffffff">);</span> </code></span></span>
explain:
Direction information refers to the rotation angle of the equipment with the axis of z, x and y respectively relative to the horizontal initial direction, and the corresponding values are alpha, beta and gamma. watchOrientation obtains the direction information of the device every fixed time and returns it through the successCB callback function. The time interval for obtaining equipment direction information can be set through the frequency parameter of option. If the direction information acquisition fails, call the callback function errorCB.
Parameters:
- successCB: ( OrientationSuccessCallback ) required
Callback function for obtaining device direction information successfully
- errorCB: ( OrientationErrorCallback ) optional
Failed to get device direction information callback function
- option: ( OrientationOption ) optional
Monitor the parameters of equipment direction information, such as the frequency of updating data, etc
Return value:
Number: used to identify the direction information listener, which can be cancelled by clearWatch method.
Platform support:
- Android - 2.2 + (supported):
support
- iOS - 4.3 + (supported):
support
Example:
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#cccccc"><!DOCTYPE html></span> <span style="color:#f0e68c"><strong><html></strong></span> <span style="color:#f0e68c"><strong><head></strong></span> <span style="color:#f0e68c"><strong><meta</strong></span> <span style="color:#bdb76b"><strong>charset</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"utf-8"</span><span style="color:#f0e68c"><strong>></strong></span> <span style="color:#f0e68c"><strong><title></strong></span><span style="color:#ffffff">Orientation Example</span><span style="color:#f0e68c"><strong></title></strong></span> <span style="color:#f0e68c"><strong><script</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"text/javascript"</span><span style="color:#f0e68c"><strong>></strong></span> <span style="color:#87ceeb">// After expanding API, call onPlusReady callback function </span><span style= "color:#ffffff" > after loading. document</span><span style="color:#ffffff">.</span><span style="color:#ffffff">addEventListener</span><span style="color:#ffffff">(</span> <span style="color:#ffa0a0">"plusready"</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>false</strong></span> <span style="color:#ffffff">);</span> <span style="color:#87ceeb "> / / after loading the extension API, you can call the extension API normally</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">orientation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">watchOrientation</span><span style="color:#ffffff">(</span> <span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#ffffff">(</span><span style="color:#ffffff"> o </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff"> alert</span><span style="color:#ffffff">(</span> <span style="color:#ffa0a0">"Orientation\nAlpha:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> o</span><span style="color:#ffffff">.</span><span style="color:#ffffff">alpha </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\nBeta:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> o</span><span style="color:#ffffff">.</span><span style="color:#ffffff">beta </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\nGamma:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> o</span><span style="color:#ffffff">.</span><span style="color:#ffffff">gamma </span><span style="color:#ffffff">);</span> <span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#ffffff">(</span><span style="color:#ffffff"> e </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff"> alert</span><span style="color:#ffffff">(</span> <span style="color:#ffa0a0">"Orientation error: "</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message </span><span style="color:#ffffff">);</span> <span style="color:#ffffff">}</span> <span style="color:#ffffff">);</span> <span style="color:#ffffff">}</span> <span style="color:#f0e68c"><strong></script></strong></span> <span style="color:#f0e68c"><strong></head></strong></span> <span style="color:#f0e68c"><strong><body></strong></span><span style="color:#ffffff"> Orientation watch </span><span style="color:#f0e68c"><strong></body></strong></span> <span style="color:#f0e68c"><strong></html></strong></span> </code></span></span>
Precautions for using plus in uni app
Turn off the direction information of the monitoring device
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">orientation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clearWatch</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> watchId </span><span style="color:#ffffff">);</span> </code></span></span>
Parameters:
- watchId: (Number) required
The direction listener ID that needs to be cancelled, and the return value of calling the watchOrientation method.
Return value:
void: None
Platform support:
- Android - 2.2 + (supported):
support
- iOS - 4.3 + (supported):
support
JSON object, monitoring device direction sensor parameters
Properties:
- frequency: (type Number) the time interval for updating direction information
Value type, unit: ms, default value: 500ms.
JSON object, device direction information data
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#f0e68c"><strong>interface</strong></span> <span style="color:#98fb98">Rotation</span> <span style="color:#ffffff">{</span> <span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>float</strong></span><span style="color:#ffffff"> alpha</span><span style="color:#ffffff">;</span> <span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>float</strong></span><span style="color:#ffffff"> beta</span><span style="color:#ffffff">;</span> <span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>float</strong></span><span style="color:#ffffff"> gamma</span><span style="color:#ffffff">;</span> <span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>float</strong></span><span style="color:#ffffff"> magneticHeading</span><span style="color:#ffffff">;</span> <span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>float</strong></span><span style="color:#ffffff"> trueHeading</span><span style="color:#ffffff">;</span> <span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>float</strong></span><span style="color:#ffffff"> headingAccuracy</span><span style="color:#ffffff">;</span> <span style="color:#ffffff">}</span> </code></span></span>
Properties:
- alpha: (float # type) the rotation angle centered on the z direction
Floating point number type, read-only attribute, with a value range of 0 to 360 (not equal to 360).
- beta: (float # type) rotation angle with x direction as axis
Floating point number type, read-only attribute, with a value range of - 180 to 180 (not equal to 180).
- gamma: (float # type) the rotation angle centered on the y direction
Floating point number type, read-only attribute, with a value range of - 180 to 180 (not equal to 180).
- Magnetic heading: (float # type) the angle between the equipment direction and the north pole of the earth's magnetic field
Floating point number type, read-only attribute, with a value range of 0 to 360 (not equal to 360).
Platform support
- Android - 2.2 + (supported)
- iOS - 4.3 + (supported):
iTouch and iPad devices do not support
- trueHeading: (float # type) the angle between the equipment direction and the true north pole direction of the earth
Floating point number type, read-only attribute, with a value range of 0 to 360 (not equal to 360).
Platform support
- Android - 2.2 + (supported)
- iOS - 4.3 + (supported):
iTouch and iPad devices do not support
- headingAccuracy: (float # type) error value of equipment direction value
Floating point number type, read-only attribute, with a value range of 0 to 360 (not equal to 360).
Platform support
- Android - 2.2 + (supported)
- iOS - 4.3 + (supported):
iTouch and iPad devices do not support
Callback function for obtaining device direction information successfully
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> onSuccess</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> rotation </span><span style="color:#ffffff">){</span> <span style="color:#87ceeb">// Get orientation success code.</span> <span style="color:#ffffff">}</span> </code></span></span>
Parameters:
- rotation: ( Rotation (required) direction information of the equipment
Return value:
void: None
Callback function failed to get device direction information
<span style="background-color:#333333"><span style="color:#474747"><code> <span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> onError</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> error </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span> <span style="color:#87ceeb">// Get orientation error code. </span> <span style="color:#ffffff">}</span> </code></span></span>
Parameters:
- error: (Exception) required; failure information
Return value:
void: None