Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
adding initial compass support
  • Loading branch information
imhotep committed Apr 3, 2012
1 parent c0b0889 commit 628e8ea626b5b24eba56f03c483fcd48dd32e177
Showing 4 changed files with 111 additions and 19 deletions.
@@ -58,8 +58,21 @@ <h3>Geolocation</h3>
<span id="geolocation"></span>
</div>
</li>
<li>
<h3>Compass</h3>
<div>
<button type="button" class="btn" onclick="getCurrentHeading();">getCurrentHeading</button>
</div>
<div>
<button type="button" class="btn" id="compassBtn" onclick="toggleCompass();">watchHeading</button>
</div>
<div>
<span id="heading"></span>
</div>
</li>
</ul>
<script type="text/javascript" src="./js/accelerometer.js"></script>
<script type="text/javascript" src="./js/compass.js"></script>
<script type="text/javascript" src="./js/device.js"></script>
<script type="text/javascript" src="./js/sample.js"></script>
</body>
@@ -39,10 +39,9 @@ navigator.accelerometer = {

errorCallback(err);
};
var watchID = deviceapis.accelerometer.watchAcceleration(success, error);
return watchID;
return deviceapis.accelerometer.watchAcceleration(success, error);
},
clearWatch: function(watchId) {
deviceapis.accelerometer.clearWatch(watchId);
clearWatch: function(watchID) {
deviceapis.accelerometer.clearWatch(watchID);
}
};
@@ -1,34 +1,53 @@
function CompassHeading(magneticHeading, trueHeading, headingAccuracy, timestamp) {
function CompassHeading(magneticHeading, trueHeading, headingAccuracy) {
this.magneticHeading = magneticHeading;
this.trueHeading = trueHeading;
this.headingAccuracy = headingAccuracy;
this.timestamp = timestamp;
this.timestamp = new Date();
}

navigator.compass = {
getCurrentHeading: function(compassSuccess, compassError, compassOptions) {
if(deviceapis.orientation == undefined) {
console.log("navigator.compass.getCurrentHeading", "Operation not supported!")
return -1;
}
var success = function(orientation) {
var compassHeading = new CompassHeading(orientation.alpha);
var compassHeading = new CompassHeading(orientation.alpha, orientation.alpha, 0);
compassSuccess(compassHeading);
};
var error = function(orientation) {

var error = function(error) {
compassError(error);
};
if(deviceapis.orientation) {
deviceapis.orientation.getCurrentOrientation(success);
} else {
console.log("navigator.compass.getCurrentHeading", "Orientation not supported!");
}
deviceapis.orientation.getCurrentOrientation(success, error);
},
watchHeading: function() {
watchHeading: function(compassSuccess, compassError, compassOptions) {
if(deviceapis.orientation == undefined) {
console.log("navigator.compass.watchHeading", "Operation not supported!")
return -1;
}
var success = function(orientation) {
var compassHeading = new CompassHeading(orientation.alpha, orientation.alpha, 0);
compassSuccess(compassHeading);
};
var error = function(error) {
compassError(error);
};
var orientationOptions = null;

if (compassOptions != undefined) {
orientationOptions = {
minNotificationInterval: compassOptions.frequency || 100
};
}
return deviceapis.orientation.watchOrientation(success, error, orientationOptions);
},
clearWatch : function() {

clearWatch : function(watchID) {
deviceapis.orientation.clearWatch(watchID);
},
watchHeadingFilter: function() {

console.log("navigator.compass.watchHeadingFilter", "Operation not supported!");
},
clearWatchFilter: function() {

console.log("navigator.compass.clearWatchFilter", "Operation not supported!");
}
}
@@ -104,4 +104,65 @@ function clearPositionWatch() {
document.getElementById("geolocation").innerHTML = "";
console.log("clearPositionWatch");
}
}

/*
* Compass
*/

var compassWatchID = null;

function getCurrentHeading() {
var successCallback = function(heading) {
var headingEm = document.getElementById("heading");
headingEm.innerHTML = "magnetic Heading: "+ heading.magneticHeading +
" true Heading: " + heading.trueHeading +
" heading accuracy: " + heading.headingAccuracy +
" heading timestamp " + heading.timestamp;
};

var errorCallback = function(error) {
console.log(error);
var headingEm = document.getElementById("heading");
headingEm.innerHTML = "ERROR";
};
navigator.compass.getCurrentHeading(successCallback, errorCallback);
}

function toggleCompass() {
var geoBtn = document.getElementById("compassBtn");
if(geoBtn.innerHTML == "watchHeading") {
watchHeading();
compassBtn.innerHTML = "clearWatch";
} else {
clearHeadingWatch();
compassBtn.innerHTML = "watchHeading";
}
}

function watchHeading() {
var successCallback = function(heading) {
console.log("watchHeading::successCallback");
var headingEm = document.getElementById("heading");
headingEm.innerHTML = "magnetic Heading: "+ heading.magneticHeading +
" true Heading: " + heading.trueHeading +
" heading accuracy: " + heading.headingAccuracy +
" heading timestamp " + heading.timestamp;
};

var errorCallback = function(error) {
console.log(error);
var headingEm = document.getElementById("heading");
headingEm.innerHTML = "ERROR";
};
compassWatchID = navigator.compass.watchHeading(successCallback, errorCallback);
console.log("watchHeading "+compassWatchID);
}
function clearHeadingWatch() {
if(compassWatchID != null) {
navigator.compass.clearWatch(compassWatchID);
compassWatchID = null;
document.getElementById("heading").innerHTML = "";
console.log("clearHeadingWatch");
}
}

0 comments on commit 628e8ea

Please sign in to comment.