【小(xiǎo)編推薦】phonegap♥•≤ 的(de)指南(nán)針api Compa>₽£ss

2014-05-21   |  ☆γ;  發布者:梁國(guó)芳   <₩♦; |   ¶♠β;查看(kàn):3320次

app開(kāi)發
一(yī)、Compass 介紹方法參數δ€₹(shù)

   &nb™∞ ↑sp;  Compass 也(yě)就(jiù)是(↔€shì),常說(shuō)的(de)指南(nán)針,又(yòu)叫羅盤

     ‍×; 方法
     ×→>;     c<∑×ompass.getCurrentHeading
     &nb&↕₩sp;    comΩ<α€pass.watchHeading

    &nb★→ ‌sp;     cσ★ ompass.clearWatch
    ε ↕‍;   &nbλ Ωαsp;  compass.watchHeadingFil×  ter (obsolete)
    &nbs™∏p;   &§ε βnbsp; compass.clearWat∏λ¶®chFilter (obsolete)

     ↕•δ 參數(shù)
    &n•♦bsp;   &∞φ ¥nbsp; compassSuccess

     δ←;     compassErr©☆"₹or
      &n'≥≥bsp;   compasα≥ ↔sOptions
      ✔ε♠;    compassHeading

二、compass.getCurrentHeading ✔≥•‌   &n✔↕≈bsp;    &nb↕ sp;   對(duì)象

改對(duì)象主要(yào)獲取羅盤的(de)當☆↔±前朝向。

navigator.compass.getCurrentHeading(cπ•σompassSuccess, compassError, comp ♣ §assOptions);

compassSuccess 成功的(de)回調函數(♦₹×shù)
compassError 失敗的(de)回調函數(shù)
compassOptions 獲取指南(nán  )針信息時(shí)的(de)一(yī)個(gè)選項例如(rú):頻(pín♥​$¶)率,它一(yī)般為(wèi)一(yī)個(gè)‍₹‌♦json   &nbs♦≠£₹p;     &‍±™$nbsp;     ✔®¶;   &nb'↓sp;     φ₩↓;   &₹✔γnbsp; 對(duì)象在

 (compass.watchHeading )中↓×‌用(yòng)

function onSuccess(heading) {
    alert('Headin☆≤Ωg: ' + heading.magneticHeading);

----------------------- Page 2‌ ↕™----------------------¥&-

};
function onError(error) {

     alert('Compass↓©✘∑Error: ' error.code);
};

navigator.compass.getCur ↔Ω♦rentHeading(onSuccess, onErr™₽¥or);

回調成功後會(huì)用(yòng)到(dàoφγ±↕)compassHeading 對(duì)象

     magneti•↓λ↓cHeading:羅盤在某一(yī)時(shí)刻的(de)朝向,'♣取值範圍是(shì)0 - 359.99 度。(數(sh♠÷ù)字類型)
     t'∞φ§rueHeading :羅盤在某一(yī)時(shí)刻相(xià ←≥×ng)對(duì)于北(běi)極的(de)朝向,取值範✘☆圍是(shì)0 - 359.99 度。如(rú)果是(shì)負φε
值則表明(míng)該參數(shù)不(bù)确定。 ±(數(shù)字類型)

    £≈↕₹; headingAccuracy:實際度數(shù)γ≈φ↔和(hé)記錄度數(shù)之間(jiān)的(de)偏差。(數(shù)∏★§♣字類型)
      "timestamp:确定羅盤朝向的(de)時(εσshí)刻.。(毫秒(miǎo)數(shù)®$π↑)

簡單列子(zǐ):

<!DOCTYPE html>
<html>
<head>

<title>Compass Example&l§∑t;/title>
<script type="text/javas εcript" charset="u✔•tf-8" src="•σphonegap.js"></script>
<script type="text/javascr÷★ipt" charset="utf-8&q ↑uot;>

   &n£ bsp; // 等待加載PhoneGap
     docume±φ™→nt.addEventListener("devi™λ&ceready", onDeviceReady,σ☆ ↔ false);
     // PhoneG→¶ε→ap 加載完畢

     function onD"±<eviceReady() {
    &nbs&‌p;    ‌" ;  navigator.compass.ge>↔tCurrentHeading(onSuccess, onError);
   &nbs₹β♣p; }

     // onS≥↑₽uccess:   返回當 ​​÷前的(de)朝向數(shù)據
   &nbs←€Ωp; function onSuccess(heading)∏₹₩ {
    ™©      ale×♥¥rt('Heading: ' + heading.magn↓γ←£eticHeading);

   &nbs'λ♠♥p; }
     // onError:&n‌λbsp; 返回朝向數(shù)據失敗
     function onE₹♥rror(compassError) {

    $‌;   &n↕σbsp;  alert('Compass∞± Error: ' + compassError.code);
     }
</script>

</head>
<body>
     <h‌←∑ 1>Example</h1&♦₽gt;

     <p>get‍'CurrentHeading</p>

----------------------- Page ‌"∏3--------------------₹♥↓Ω---

</body>
</html>

三、compass.watchHeading 對(duì" )象

說(shuō)明(míng):

羅盤是(shì)一(yī)個(gè)檢測設備方向或朝向的(de)傳∏"​感器(qì),使用(yòng)度作(zuò)↕♣為(wèi)衡量單位,取值範圍從(cóng)0 度到(dào)359.99 φ÷度。

compass.watchHeading 每隔固定時(s±±hí)間(jiān)就(jiù)獲取一(y&☆→ī)次設備的(de)當前朝向。每次取得(de)朝向後,headingSucc$↕πess 回調函

數(shù)會(huì)被執行(xíng)。通(tōng)過com★ε×<passOptions 對(duì)象的(de)frequency 參數(s λhù)可(kě)以設定以毫秒(miǎo)為(wèi)單位的>₽"÷(de)時(shí)間(jiān)間(jεδiān)隔。

返回的(de)watch   ID 是(sh™&↑ì)羅盤監視(shì)周期的(de)引用(yòng),可(kě)以通(↓↔♣₽tōng)過compass.clearWatch ‌©​"調用(yòng)該watch ≤​ ↓  ID  以停止對(duì)羅盤的(de♣≈∑)監

視(shì)。

var watchID = naviga"σ> tor.compass.watchHeading(compassSucσ​♥¥cess,
      γ¥   &nbs ♥ 'p;   &nbs±♥∞≤p;     &£ nbsp;   &nb≤ ✘​sp;  compassError, [compassOptio‌ ×ns]);

var options = {

     frequen≥δ→£cy: 3000
};
var watchID = navigator.comφπ★←pass.watchHeading(onSucc §ess, onError, options);

通(tōng)過clearWatch 來(lái)停止對(duì ☆)羅盤的(de)檢測

watchID:由compass.watchHeading ✔©β返回的(de)引用(yòng)标示。

navigator.compass.clα♥λearWatch(watchID);

<!DOCTYPE html>>★δ♣;

<html>

<head>

<title>Compass Exampl↓→​>e</title>

<script type="text/jε₹✘♦avascript" charset=≠≤"utf-8" src="phδ ®♠onegap.js"></script>

<script type="≠®;text/javascript" ¥πφ; charset="utf-8& ÷×quot;>

     // watchID 是(sλ hì)當前“watchHeading &r&∞dquo;的(de)引用(yòng)

     var₽  watchID = null;

     // 等>σ待加載PhoneGap

   &n♠¥"bsp; document.addEventListener('§"deviceready&quo​>''t;, onDeviceReady, false);

   &nγ₽bsp; // PhoneGap 加載完畢

     ↔→ε function onDeviceRead£Ω±y() {

      &n'<σbsp;    startWatch(¥ Ω);

     €; }

----------------------- Page 4-♥$₽----------------------

      //&nbβ↕×sp; 開(kāi)始監視(shì)羅盤

      functioλ  n startWatch() {

   &nbs&δ p;     '←;   //  每隔3"≤₽ 秒(miǎo)鐘(zhōng)更新一(yī)次羅盤數(✘✘shù)據

   &nbs ←±$p;   &nπ×bsp;    var opti ♠ons = { frequency: 3000 }↔®;

    & €§nbsp;    ↓¶   watchID✔γ✘ = navigator.compass.watch✔★Heading(onSuccess, on±>‌Error, options);

      }‍ 

   &nbs$εp;  //  停止監視(shì)羅盤

   &nb≠☆↓sp;  function s‍↓topWatch() {

    &∑Ω•nbsp;     &nb₽•←≥sp; if (watchID) {

    ✘α   &nbs♠≥p;      ←♣↕‍     navigator.co ✔mpass.clearWatch(watchID);

    &">✘"nbsp;   $$;      ¶₩;    watchID = nul₩>♠l;

      &n™"bsp;     }→•★>

      }$↓

    ¶"♦  // onSuccess:  ®↑ ® 返回羅盤的(de)當前朝向

    &nbs<✔÷p; function onSuccess(♥>heading) {

     &n≈'bsp;    & ​↕→nbsp; var element = document.getElem¶< entById('heading');

   &nb★λsp;     ♠♣£>   element.i∏÷nnerHTML = 'Heading: ' + heading.magne←​♦ticHeading;

       <☆£}

     ♠₩&→ // onError:  獲取羅♥'盤朝向失敗

    &"≥α₩nbsp; function onError(compassError)↕  {

     ★<©λ;   &nb✔✔sp;   alert('Com±•±pass error: ' + compassError.cod₽÷e);

      }

</script>

</head>

<body>

    <   <div id="heading&quoε↔t;>Waiting for heading.‌>≈♥..</div>

    &n✔ ₩♠bsp; <button"startWa£‍δtch();">Start Watching< φβ ;/button>

      <but&ε☆$ton"stopWatch();">St↔&op Watching</button>

</body>

</html>