【小(xiǎo)編推薦】PhoneGap加速計(jβ✘♣ì)api 獲取加速計(jì)x 軸 y 軸 z軸γ♣信息實現(xiàn)搖一(yī)搖、重力感應

2014-05-21   |≈ε   發布者:梁國(guó©σ±✘)芳   | &n÷↑bsp; 查看(kàn):3320次

app開(kāi)發
一(yī)、PhoneGap 加速計(j&λ♠ì)Accelerometer   &nb✘€sp;   §β ₽      ‍↕;    &nb₽€∑≤sp; 對(duì)象介紹

主要(yào)方法

accelerometer.getCurrentAccelerat ™'ion 獲取當前設備在x,y,z 軸上(shàng)的(‍↑βde)加速度信息
accelerometer.watchAcceleration Ω←±£定期獲取設備的(de)加速度信息
accelerometer.clearWatch  &n ÷bsp;     停止定期 ♣<×獲取設備的(de)加速度信息

參數(shù)
accelerometerSuccess 獲取加速度信息γ♥成功的(de)回調函數(shù)

accelerometerError 獲取加速度信息失敗☆→的(de)回調函數(shù)
    acceε"©↑lerometerOptions
    一(yī)般為(wèi♥♣≥)json 對(duì)象,frequency 是(shì)它↑•目前的(de)唯一(yī)參數(shù),以毫秒(miǎo)數(shù♠™₽)表示,用(yòng)來(lái)指定定期‌‍★★獲取

加速度信息頻(pín)率

二、獲取加速度信息

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="‌α→γ;Content-Type" content=&qu≠©←£ot;text/html; charset=utf-8">

<title>Acceleration 例子(zǐ)&¥¥↑lt;/title>

<script type="text/javascript&q​© uot; charset="utf-8"•β$ ; src="cordova.js&≥®$ quot;></script>

<script type="text/ja↑₹vascript" charset=&'♠₹"utf-8">

----------------------- P♦∞∞age 2-----------------------♦σ> 

     // 等待PhoneGaσ↑​p 加載

document.addEventListener("deviceλ₹±&ready", onDeviceReady, false);

     // 加←$載完成

   &nb≥₹sp; function onDeviceReady() {

navigator.accelerometer.getC ↑™©urrentAcceleration(onSu£‌ccess, onError);

     }∑∞

     // 獲取加速度§©信息成功後的(de)回調函數(shù)

     // 接收包含當前加速¥™'±度信息的(de)Acceleration 對(duì)象

     functioγ✔↕n onSuccess(acceleration) {

     &n↔<‍bsp;    alert('X 軸方向的←>"α(de)加速度: ' + acceleration.x + '\">n' +

     &n<±λ™bsp;    &nb®∞αsp;   &nbs↔↑₽p;  'Y 軸方向的(de)加速度: ' + acו✔↓celeration.y + '\n' +

    &n♦λλbsp;    &nbs★£πp;      δΩ; 'Z 軸方向的(de)加速度: ' + acc≠λ'★eleration.z + '\n' +

   &nbs>₹±p;    €←;      ♦¶↕©   '時(shí)間(jiān)戳: ' π♣←;     &n×α↓bsp;   + acceleration.times$¶♥tamp + '\n');

     }

     // 獲取加速度信 Ω息失敗後的(de)回調函數(shù)

     fun¥÷∑ction onError() {

      &n<∏•¶bsp;   alert('出錯(cuò)了(le)!♥®');

   &nb>¥β∑sp; }

</script>

</head>

<body>

<h1>例子(zǐ)</h1>

<p>獲取當前的(de)加速度"÷信息</p>

</body>

</html>

三.定期獲取加速度信息

<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Conteγ↑‍nt-Type" content="‍σ;text/html; charset=utf-8&qu±"×σot;>

<title>Acceleration &n→γ₹®bsp;  例子(zǐ)</title&λ≥α☆gt;

<script type="te≈ ♣xt/javascript" cha←©‍rset="utf-8" src=✘σ'"cordova.js">&l&&₽​t;/script>

<script type="text/javasc₹α÷'ript" charset="utf-8"&δ↕ ‍gt;

----------------------- Page 3------- ©----------------

     /←≈/  當前watchAccelerati≈‌on  的(de)引用(yòng)ID

varwatchID = null;

     //  等待Pho↕≤γneGap 加載

document.addEventListener("deviπ​αceready", onDeviceReady ≥↔£, false);

//  加載完成

    ∑★;  function onDeviceReady() {
startWatch();
    &•γ nbsp; }

    ₽✔ ♦; //  開(kāi)始監測
     "α± function startWatch() {

     &‍£♥∏nbsp;     //&nbs' ​₽p; 每隔三秒(miǎo)更新一(yī)次信息
var options = { frequency: 3000 };

watchID = navigator.acceler₹↓δometer.watchAcceleration(onSucc≥γess, onError, options);
    &nb↕↓sp; }

     // ≤™; 停止監測
    ♥​;  function stopWat₽€ch() {

     ×←   &nb> sp;  if (watchID) {
navigator.accelerometer.clearWatch(watλ<™♥chID);
watchID = null;

      &nπ÷bsp;   ‌≥γ§; }
   &nbs←¶βp;  }

    ☆β //  獲取加速度信息成功後的(de)回調函數(sh<₩↕ù)
     •™//  接收包含當前加速度信息∞ 的(de)Acceleration 對(duì)象↑±₽
    $•;  function onSuccess(acc♥€eleration) {

var element = documen★π't.getElementById('accelerometer');
element.innerHTML = 'X 軸方λ'₹¶向的(de)加速度: ' + accel∑&eration.x + '<br />' +
      &£★ πnbsp;     δ↔$¶     β ;     ←     →♣     ∑" §;      ¶" ©; 'Y 軸方向的(de)加速度: ' + acceleration.y + ✔ε§>'<br />' +

    &↑‌ε↔nbsp;     &nb&♣πsp;   &nbs​₽±÷p;   &nbs≤→Ω∑p;   >↑‍      φ'¶;      ♥σ±    'Z 軸方向的(de)加速度: $απ' + acceleration.z + '<br />'ε∑σ +
    &nb≤₽≥sp;     &₽↑✔nbsp;     ↑↓;     &nγ™bsp;     &n×→<÷bsp;   &nbs☆"p;      '時↔≠σ(shí)間(jiān)戳: '   ♣$↓     &n♠∑bsp;   + acce>£↕leration.timestamp +↓→ '<br />';
      }

     //  獲取加速γ₹度信息失敗後的(de)回調函數(shù)
   &nbsλ£p;  function onError() {

alert('onError!');
     ✘  }

----------------------- Page 4----©≠↕♥-------------------

</script>

</head>
<body>
<div id="accelerometer">✔☆™;監測加速度信息中...</divγ★>

<button"stopWatch();"​∞‌>停止監測加速度信息</button>♠ ¥;
</body>
</html>

 四.搖一(yī)搖

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv=&qu☆ ot;Content-Type" cont↑§π☆ent="text/html; charset☆ ≤=UTF-8">

<title>Acceleratio§≥≥n    例子(₹♠zǐ)</title>

<script type="σβ<text/javascript" charset≤Ω="utf-8" src="cord×₩↔↑ova-1.5.0.js"></s±§±cript>

<script type="tφ∑ext/javascript" charse$♦t="utf-8">

varwatchID = null;

document.addEventListener(&quo©λt;deviceready",'✘ onDeviceReady, false);

    ¥σ€  varoldValue = {

   &nb★§↕sp;  x: null,

    ☆♣€  y: null,

     ♥≥;  z: null

       }

    &nbs‌✔p; function onDeviceReady() {

startWatch();

    &nbs• ✔p; }

     λ‍σ; function startWatch() {

var options = { frequency: 300 };

watchID = navigator.accel‌δ•φerometer.watchAcceleration(onSucc★ δess, onError, options);

      }

     ✘←λ∏; function stopWatch() {

   &nb¥"sp;   &nbs§✔p;   if (watchID) {₹₹∑

navigator.accelerometer.clearWatc§§±h(watchID);

--------------------β≈$±--- Page 5-----------------------

watchID = null;

    &nb→ ≠sp;   &nbλ<sp;   }

    ™↕σ  }

     ₹↔ ✔ //  獲取加速度信息成功後的(de)回調函數(shù)¶↓∞£

    &n>↑‌>bsp; function onSuccess(newValue) {φ 

       ÷✘var changes = {},

     &nb♣♦¥®sp;     &nb÷"$πsp;  bound = 3;

      &n<φ↔γbsp;    ±←   if (oldVal✘∑ue.x !== null) {

    &n  ‌$bsp;  &nbs ↓ ​p;    &nbs∑∏♦ p; changes.x = Math.abs(ol♣•"dValue.x, newValue.x);

     &nbs‍ $p;     &nb×≥λ→sp;  changes.y = Math.abs™≈¶✔(oldValue.y, newValue•≠≠♠.y);

      &•σnbsp;    &n©•bsp;  changes.z = Math.abs(oldVa÷φ>πlue.z, newValue.z);

    &nbsγββ¶p;   ✔δ¶÷      }

    &nb♦ ≤λsp;    →©     if (changes.↕×x> bound &&changes.y&↓↓gt; bound &&ch≥₩anges.z>

     &nbs €σ​p;    ₽δ    boun♦ε $d) {

    &nbsπ↓p;      <α₩;   alert('檢測到(dào)手機(j©>ī)晃動');

    &nb÷​≠$sp;    &n>‌&bsp;    }

     &↑↓‌nbsp;    ✔↓$;    oldValue = ✔£•→{

     &♦£nbsp;   &nbsφ≥≤δp;   ✔•≈↓ x: newValue.x,

      'γ₩      •δ>₹;  y: newValue.y,

    &nb™φsp;   & <©nbsp;   π™λ↔  z: newValue.z

   &nb™©™÷sp;    &nb∞↕¶sp;     →; }

var element = document.getEle™↑✔♠mentById('accelerometer');

element.innerHTML = 'Acceleration ↑$X: ' + newValue.x + '<br />​←←®' +

   &nb★‍∑☆sp;    &nbs‌×$p;      ✘∑✘¥;    >×§   &nπ≈§bsp;     &nb‍'∑ sp;     &n↑✔bsp;   & >nbsp;  'Acceleration Y:‍₩₩ ' + newValue.y + '<br />' +

    &nbs♣☆p;    '♦♠♠     'εγ;     &n₹γbsp;     ε♠;   &<×nbsp;     &nbs≈δp;   ↔βε   'Acceleration Z: ' + neδ→™wValue.z + '<br />' +

    ₹↕ ×;    &↑✘↔βnbsp;     &§♠γnbsp;   &n'♠±bsp;   &©₽>nbsp;     &nbs↑↑®∞p;   &nb ↓≈sp;     &nb↕$sp;  'Timestamp: '  &n¥↔φ"bsp;    &nbs↑÷§"p;   + newValue.timestamp +≤✘∏ε '<br />';

    &n♦​∞¶bsp; }

   &nb‍ ×÷sp;  //  獲取加速度信息失∏€™敗後的(de)回調函數(shù)

      €♠&¶function onError() {

alert('onError!');

    &nγδ☆'bsp; }

</script>

</head>

<body>

<div id="accelerometer&≤ ♦βquot;>監測加速度信息中...</div><↔↓

<button"stopWatch•ε();">停止監測加速度信息&₽&lt;/button>

</body>

</html>

----------------------- Page 6-$ •≥----------------------

五.重力感應

<!DOCTYPE HTML>

<html>
<head>
<meta http-equiv="Content-€★₹♥Type" content="text/ht→™ml; charset=UTF-8">"↕∞♦;

<title>Acceleration  $≠   例子(zǐ)</title>
<style type="text/cssφ✘">
     ©↓ #ball {

      ×÷position: absolute;
    &×₩↑£nbsp; z-index: 0;
    & ≤→≥nbsp; left: 20px;

     γασ ; top: 100px;
   &nb " sp;  background: #f00;
}

</style>
<script type="te ©∞≈xt/javascript" c★φ≤≈harset="utf-8" src="cord↓​ova-1.5.0.js"></script>
<script type="text/java¶§Ωscript" charset=" ¥•utf-8">

varwatchID = null;

document.addEventListener(φ≤ε"deviceready", onDevi‍≥ceReady, false);

   &n☆φ✔bsp;  function drawCi™"σ☆rcle(x,y) {

      va$£₩→rctx = document.getElementById(& €β≈quot;ball").getContext('2d↔♥ ');
    &nb₹∏≠βsp; varrd = 10;
   &n‌≠ bsp;  ctx.beginPa€ γth();

     ↑↓     &nbββ₽​sp;  ctx.arc(x, y, rd, δ☆↑0, Math.PI * 2, false);
      ctx.clo©≤←§sePath();
   &nbs₹♦p;  ctx.fillStyle =•£ "yellow";

      ctx.fλ δ¥ill();
      }

      funcλδtion clear() {
      va‍≠'₹rctx = document.getElementBy€←​Id("ball").getContext('2d'€‌ε);
     ✘δ ctx.clearRect(0,0,200,200);

      }

   &nbs↔←>p;  function onDevic∏¥→"eReady() {

      drβ™λ☆awCircle(10,10);
startWatch();

-----------------------₩ & Page 7--------------------- ≈--

      }

    &nγ§'₽bsp; function startWatch() {
var options = { frequency: 4∑&★0 };

watchID = navigator.accelerom→"λ₹eter.watchAcceleration(onSuccγ‌π←ess, onError, option∏→ s);
   &nbδδλ☆sp;  }

      fu♦≤↓♠nction stopWatch() {
     &nε¥bsp;      ×™©✘if (watchID) {

navigator.accelerometer.clearWa≥"♥ tch(watchID);
watchID = null;
   &n©×α✔bsp;     ™↔÷   }

   &nb↓♦πsp;  }

     '♣β  varoldX = 10, oldY = 10;

    ♦<;  //  獲取加速度信息成功後的(deλγ≠✘)回調函數(shù)
     "≠©≈ function onSuccess(newVal↑×≠ue) {
    &​$♦nbsp;     &nb♥♥₹←sp; if(10<=oldX<180&α÷≥&10<=oldY<180){

     &nb←×±×sp;   δγβ      '₽α;   clear(≠€₽λ);
     ​§¶≥      >λ≤       dr✘δΩ©awCircle(oldX,oldY);
     &nbs  p;   &nbs‌•σp;  }

      &nb ≤✔☆sp;   ¶$✘;  oldX -= newVa¥↔♥♥lue.x;
     &nb ✔₹™sp;      ∏€αoldY += newValue.y;
var element = document.getElementByIα ₩"d('accelerometer');

element.innerHTML = σΩ'X: ' + oldX + '<br /λ$↑$>' +
     ≠•     ₹®     &nπ$bsp;   &nbsφ£÷☆p;    &♣→₩♥nbsp;  &nbs♣<•←p;     &nbs​γp;     &nb$↔÷sp; 'Y: ' + oldY + '<br />'‍×≈ ;
     ₩$; }

    &nb∏✘sp; //  獲取加速度信息失敗後的(de)回調函數(shù)
      functi™↑'on onError() {

alert('onError!');
   &nβ"bsp;  }

</script>
</head>
<body>

<div id="accelerometer"↕☆↔;>開(kāi)啓重力感應...<δ‌£σ/div>
<button"stopWatch();">₽π;停止重力感應</button>
<canvas id="ball" width∏π∞σ="200" height="20¥× 0"></canvas>

</body>
</html>