PhoneGap-Accelerometer

采集设备在x、y、z方向上的动作。

    accelerometer.getCurrentAcceleration
accelerometer.watchAcceleration
accelerometer.clearWatch


    accelerometerSuccess
accelerometerError
accelerometerOptions


Acceleration


[b][size=medium]accelerometer.getCurrentAcceleration[/size][/b]
返回当前沿x、y和z方向的加速度。
navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);

说明:
加速计是检测设备在当前方向上所做相对运动变化(增、减量)的运动传感器。加速计可以检测沿X、Y和Z轴的三维运动。
加速度数据通过accelerometerSuccess回调函数返回。
支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
}
function onError() {
alert('onError!');
}
navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);

<!DOCTYPE html>
<html>
<head>
<title>Acceleration Example</title>

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">

// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap加载完毕
function onDeviceReady() {
navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
}

// onSuccess: 返回当前加速度数据的快照
function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
}

// onError: 返回加速度数据失败
function onError() {
alert('onError!');
}
</script>
</head>
<body>
<h1>Example</h1>
<p>getCurrentAcceleration</p>
</body>
</html>

iPhone的特异情况:
iPhone没有获取在任何给定点当前加速度数据的概念。
你必须通过给定时间间隔查看加速度并获得数据。
因此,getCurrentAcceleration函数会返回从phoneGap watchAccelerometer调用开始后的最近一个返回值。


[b][size=medium]accelerometer.watchAcceleration [/size][/b]

在固定的时间间隔获取沿x、y和z轴的加速度。
var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
accelerometerError,
[accelerometerOptions]);



说明:
加速计是检测设备在当前方向上所做相对运动变化(增、减量)的动作传感器。加速计可以检测沿X、Y和Z轴的三维运动。
accelerometer.watchAcceleration每隔固定时间就获取一次设备的当前加速度。每次取得加速度后,accelerometerSuccess回调函数会被执行。通过acceleratorOptions对象的frequency参数可以设定以毫秒为单位的时间间隔。
返回的watch id是加速度计监视周期的引用,可以通过accelerometer.clearWatch调用该watch ID以停止对加速度计的监视。

支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone

function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
}

function onError() {
alert('onError!');
}

var options = { frequency: 3000 }; // 每隔3秒更新一次

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);



<!DOCTYPE html>
<html>
<head>
<title>Acceleration Example</title>

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">

// watch id 是当前“watchAcceleration”的引用
var watchID = null;

// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap加载完毕
function onDeviceReady() {
startWatch();
}

// 开始监视加速度
function startWatch() {

// 每隔3秒钟更新一次加速度数据
var options = { frequency: 3000 };

watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
}

// 停止监视加速度
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}

// onSuccess: 获取当前加速度数据的快照
function onSuccess(acceleration) {
var element = document.getElementById('accelerometer');
element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
'Acceleration Y: ' + acceleration.y + '<br />' +
'Acceleration Z: ' + acceleration.z + '<br />' +
'Timestamp: ' + acceleration.timestamp + '<br />';
}

// onError: 获取加速度失败
function onError() {
alert('onError!');
}

</script>
</head>
<body>
<div id="accelerometer">Waiting for accelerometer...</div>
</body>
</html>


iPhone的特异情况:

在请求的时间间隔,PhoneGap将调用success回调指向的函数,并传递加速度计数据。
不过,PhoneGap将对设备的请求间隔时间限制为最小40ms,最大1000ms。
例如,如果你设定每隔3秒(3000毫秒)请求一次,PhoneGap仍然每隔1秒请求一次设备,但是每隔3秒才调用一次success回调函数。


[b][size=medium]accelerometer.clearWatch [/size][/b]
停止watch ID参数指向的加速度监视。

navigator.accelerometer.clearWatch(watchID);


watchID:由accelerometer.watchAcceleration返回的引用标识ID。

支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone



var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

// ... 后续处理 ...

navigator.accelerometer.clearWatch(watchID);


<!DOCTYPE html>
<html>
<head>
<title>Acceleration Example</title>

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">

// watch id 是当前“watchAcceleration”的引用
var watchID = null;

// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap加载完毕
function onDeviceReady() {
startWatch();
}

// 开始监视加速度
function startWatch() {

// 每隔3秒钟更新一次加速度数据
var options = { frequency: 3000 };
watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
}

// 停止监视加速度
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}

// onSuccess: 获取当前加速度数据的快照
function onSuccess(acceleration) {
var element = document.getElementById('accelerometer');
element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
'Acceleration Y: ' + acceleration.y + '<br />' +
'Acceleration Z: ' + acceleration.z + '<br />' +
'Timestamp: ' + acceleration.timestamp + '<br />';
}

// onError: 获取加速度失败
function onError() {
alert('onError!');
}

</script>
</head>
<body>
<div id="accelerometer">Waiting for accelerometer...</div>
<button onclick="stopWatch();">Stop Watching</button>
</body>
</html>



[b][size=medium]Acceleration [/size][/b]

包含特定时间点采集到的加速计数据。

属性:
x:在X轴的运动量,[0, 1]范围(数字类型)
y:在Y轴的运动量,[0, 1]范围(数字类型)
z:在Z轴的运动量,[0, 1]范围(数字类型)
timestamp:以毫秒为单位的创建时间戳。(DOMTimeStamp类型)

说明:
这个对象是由phoneGap创建和填充,并由Acce这个对象是由PhoneGap创建和填充,并由Accelerometer的方法返回。

支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone



function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
}

function onError() {
alert('onError!');
}

navigator.accelerometer.getCurrentAcceleration(onSuccess,onError);



<!DOCTYPE html>
<html>
<head>
<title>Acceleration Example</title>

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">

// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap加载完毕
function onDeviceReady() {
navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
}

// onSuccess: 获得加速度数据的快照
function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
}

// onError: 获取加速度失败
function onError() {
alert('onError!');
}

</script>
</head>
<body>
<h1>Example</h1>
<p>getCurrentAcceleration</p>
</body>
</html>



[b][size=medium]accelerometerSuccess [/size][/b]

提供加速度信息的onSuccess回调函数。

function(acceleration) {
// 进一步处理
}



参数:
acceleration: 在某一时刻的加速度(Acacceleration:在某一时刻的加速度。(Acceleration对象类型)

function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');




[b][size=medium]accelerometerError [/size][/b]

加速度方法的onError回调函数。
复制
function() {
// 错误处理
}



[b][size=medium]accelerometerOptions [/size][/b]

定制检索加速度计的可选参数。

选项:
frequency:多少毫秒获取一次Acceleration。(数字类型)(默认值:10000)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值