update sample application to demo compass api
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/commit/b3bb8555 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/tree/b3bb8555 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/diff/b3bb8555 Branch: refs/heads/master Commit: b3bb8555c1c96b5a655382de83c404869b2572d6 Parents: 7fd75ab Author: hermwong <[email protected]> Authored: Thu Mar 15 10:35:56 2012 -0700 Committer: hermwong <[email protected]> Committed: Thu Mar 15 10:35:56 2012 -0700 ---------------------------------------------------------------------- framework/index.html | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 45 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/blob/b3bb8555/framework/index.html ---------------------------------------------------------------------- diff --git a/framework/index.html b/framework/index.html index d5c45c3..5d2a0d0 100644 --- a/framework/index.html +++ b/framework/index.html @@ -39,6 +39,7 @@ <a onclick="storage();">Storage Tests</a> <a onclick="callPhoneGapExec();menu(false);">PhoneGap Exec</a> <a onclick="page('touchDemo');">Touch Events Examples</a> + <a onclick="page('compassDemo');">Compass API</a> <a onclick="menu(false);">Done</a> </div> @@ -96,11 +97,55 @@ <div id="storageResults"></div> </div> +<div id="compassDemo" style="display: none;"> + <p>Compass Demo</p> + <button id="btnGetCurrentHeading" onclick="displayCurrentHeading();">Get Current Heading</button> + <p>Watch Heading</p> + <button id="btnStartCompassWatch" onclick="startCompassWatch();">Start Compass Watch</button> + <button id="btnStopCompassWatch" onclick="stopCompassWatch();">Stop Compass Watch</button> + +</div> + <script> document.addEventListener('deviceready', function () { // PhoneGap is ready, do all your stuf here }, false); +var watchId = null; + +function displayCurrentHeading() { + + function successful(response) { + navigator.notification.alert("last heading: " + response); + } + + var request = navigator.compass.getCurrentHeading(successful, {}); + +} + +function startCompassWatch() { + // set frequency of compass updates in milliseconds + var options = { frequency: 5000 }; + + function onStartCompassSuccess(heading) { + navigator.notification.alert("Heading: " + heading); + } + + watchId = navigator.compass.watchHeading(onStartCompassSuccess, onCompassError, options); +} + +function stopCompassWatch() { + if (watchId) { + navigator.compass.clearWatch(watchId); + watchId = null; + } +} + +function onCompassError(compassError) { + navigator.notification.alert("Compass error: " + compassError.code); +} + + // setup event listeners for touch events document.querySelector('#btnTouchStart').addEventListener('touchstart', onTouchEvent, false); document.querySelector('#btnTouchEnd').addEventListener('touchend', onTouchEvent, false);
