Book Image

PhoneGap Beginners Guide (third edition)

Book Image

PhoneGap Beginners Guide (third edition)

Overview of this book

Table of Contents (22 chapters)
PhoneGap Beginner's Guide Third Edition
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Related Plugin Resources
Index

Accelerometer


The PhoneGap Accelerometer API allows you to detect the device movement change values relative to device orientation. Note that the accelerometer detects the values as a delta movement relative to the current device position. Even more important, it takes into consideration the effect of gravity (that is, 9.81 m/s2), so that when a device is lying flat on a table facing up, the value returned should be x = 0, y = 0, and z = 9.81.

As with any other plugin, you have to install the plugin before you can use it in your project. The plugin can be added to your project using the following command:

$ cordova plugin add cordova-plugin-device-motion

Once the plugin is installed on the project, a navigator.Accelerometer global object, is created and it is available to use once the deviceready event is fired. However, it is recommended that you still check for its presence before using it:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    if...