Device Motion

陀螺仪传感器在当前的设备方向检测三个维度 x,y,z

方法(Methods)

getCurrentAcceleration()

获取当前陀螺仪数据
返回 对象 - x, y, z coordinates + timestamp

watchAcceleration(options)

开启一个时钟间隔(毫秒) 获取陀螺仪数据

参数 类型 说明
options Object 监视数据
Options 类型 说明
frequency Number 间隔的毫秒数

返回 对象 - x, y, z coordinates + timestamp

clearWatch(watchID)

清除,停止watchAcceleration

参数 类型 说明
watchID Number watchAcceleration()返回的对象

代码实例

module.controller('ThisCtrl', function($cordovaDeviceMotion) {

document.addEventListener("deviceready", function () {

$cordovaDeviceMotion.getCurrentAcceleration().then(function(result) {
var X = result.x;
var Y = result.y;
var Z = result.z;
var timeStamp = result.timestamp;
}, function(err) {
// An error occurred. Show a message to the user
});

}, false);


// watch Acceleration
var options = { frequency: 20000 };

document.addEventListener("deviceready", function () {

var watch = $cordovaDeviceMotion.watchAcceleration(options);
watch.then(
null,
function(error) {
// An error occurred
},
function(result) {
var X = result.x;
var Y = result.y;
var Z = result.z;
var timeStamp = result.timestamp;
});


watch.clearWatch();
// OR
$cordovaDeviceMotion.clearWatch(watch)
.then(function(result) {
// success
}, function (error) {
// error
});

}, false);
});