--- a/src/main/resources/manifest/tracker.manifest Tue Aug 23 18:01:35 2011 -0400
+++ b/src/main/resources/manifest/tracker.manifest Tue Aug 23 18:12:40 2011 -0400
@@ -4,4 +4,5 @@
/styles/tracker.css
/scripts/tracker.js
/scripts/json2.js
+/scripts/Utils.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/resources/scripts/Utils.js Tue Aug 23 18:12:40 2011 -0400
@@ -0,0 +1,138 @@
+var Util = {
+
+ // Web Storage handling
+
+ store : {
+ enabled : (function() {
+ try {
+ return !!window.localStorage.getItem;
+ } catch (e) {
+ return false;
+ }
+ })(),
+ set : function (key, value) {
+ try {
+ window.localStorage.setItem(key, JSON.stringify(value));
+ return true;
+ } catch (e) { };
+ return false;
+ },
+ get : function (key) {
+ try {
+ var value = window.localStorage.getItem(key);
+ return ((value != null)? JSON.parse(value) : null);
+ } catch (e) { };
+ return null;
+ }
+ },
+
+ log : function (msg) {
+ try {
+
+ console.log(msg);
+ } catch (e) {
+ var element = document.getElementById("log");
+ if (element != null) {
+ if (element.textContent != null) element.textContent = msg;
+ if (element.innerHTML != null) element.innerHTML = msg;
+ }
+ }
+ },
+
+ // pretty formatting of numbers and time
+
+ formatNumber : function (n) {
+ return (n < 10)? "0" + n : n;
+ },
+
+ formatTime : function (time) {
+ t = parseInt(time);
+ hours = parseInt(t / 3600);
+ minutes = parseInt(t / 60);
+ seconds = t - ((hours * 3600) + (minutes * 60));
+ return ((hours > 0)? Util.formatNumber(hours) + ":": "") + Util.formatNumber(minutes) + ":" + Util.formatNumber(seconds);
+ },
+
+
+ // send information to the server as JSON
+
+ postNewResourceAsJSON : function (uri, data, successHandler) {
+ var xhr = new XMLHttpRequest;
+
+ try {
+ xhr.open("POST", uri, true);
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4) {
+ if (xhr.status != 201) {
+ Util.log("xhr failed? [" + xhr.status + "] " + xhr.statusText);
+ } else {
+ var resource = xhr.getResponseHeader("Location");
+ Util.log("resource created at " + resource);
+ if (successHandler != null) successHandler(resource);
+ }
+ } else {
+ Util.log("xhr readyState is " + xhr.readyState);
+ }
+ };
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.setRequestHeader("Content-Length", data.length);
+ xhr.setRequestHeader("Accept", "text/plain");
+
+ xhr.send(JSON.stringify(data));
+
+ } catch (e) {
+ Util.log("Failed to send to server " + e);
+ return false;
+ }
+ return true;
+ },
+
+ // Geolocation related functions
+
+ geo : {
+
+ // calculate the distance in km between two GPS locations
+ calculateDistance : function (lat1, lon1, lat2, lon2) {
+ var dLat = (lat2-lat1) * Math.PI / 180;
+ var dLon = (lon2-lon1) * Math.PI / 180;
+ var lat1 = lat1 * Math.PI / 180;
+ var lat2 = lat2 * Math.PI / 180;
+ var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
+ Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
+ // 6371 km is average earth radius
+ return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
+ },
+
+ // calculate the distance in km between several GPS locations
+ calculateDistanceCoordinates : function (events) {
+ var result = 0;
+ if (events.length > 0) {
+ var i = 1;
+ while (i < events.length) {
+ if (!!events[i-1].c && !!events[i].c) {
+ var position1 = events[i-1].c;
+ var position2 = events[i].c;
+ result += Util.geo.calculateDistance(position1[1], position1[0], position2[1], position2[0]);
+ }
+ i++;
+ }
+ }
+ return result;
+ },
+
+ // calculate the total duration between geolocation coordinates
+ calculateDurationCoordinates : function (events) {
+ var result = 0;
+ if (events.length > 0) {
+ var i = 1;
+ while (i < events.length) {
+ if (!!events[i-1].c && !!events[i].c) {
+ result += (events[i].t - events[i-1].t);
+ }
+ i++;
+ }
+ }
+ return result;
+ }
+ }
+};
--- a/src/main/resources/scripts/tracker.js Tue Aug 23 18:01:35 2011 -0400
+++ b/src/main/resources/scripts/tracker.js Tue Aug 23 18:12:40 2011 -0400
@@ -9,128 +9,6 @@
GPS_MAX_ERRORS : 2,
};
-var Util = {
- store : {
- enabled : (function() {
- try {
- return !!window.localStorage.getItem;
- } catch (e) {
- return false;
- }
- })(),
- set : function (key, value) {
- try {
- window.localStorage.setItem(key, JSON.stringify(value));
- return true;
- } catch (e) { };
- return false;
- },
- get : function (key) {
- try {
- var value = window.localStorage.getItem(key);
- return ((value != null)? JSON.parse(value) : null);
- } catch (e) { };
- return null;
- }
- },
-
- log : function (msg) {
- try {
-
- console.log(msg);
- } catch (e) {
- var element = document.getElementById("log");
- if (element != null) {
- if (element.textContent != null) element.textContent = msg;
- if (element.innerHTML != null) element.innerHTML = msg;
- }
- }
- },
-
- // pretty formatting of numbers
- formatNumber : function (n) {
- return (n < 10)? "0" + n : n;
- },
-
- // pretty formatting of time
- formatTime : function (time) {
- t = parseInt(time);
- hours = parseInt(t / 3600);
- minutes = parseInt(t / 60);
- seconds = t - ((hours * 3600) + (minutes * 60));
- return ((hours > 0)? Util.formatNumber(hours) + ":": "") + Util.formatNumber(minutes) + ":" + Util.formatNumber(seconds);
- },
-
- // send information to the server as JSON
- postNewResourceAsJSON : function (uri, data) {
- var xhr = new XMLHttpRequest;
-
- try {
- xhr.open("POST", uri, true);
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) {
- if (xhr.status != 201) {
- Util.log("xhr failed? [" + xhr.status + "] " + xhr.statusText);
- } else {
- Util.log("resource created at " + xhr.getResponseHeader("Location"));
- }
- } else {
- Util.log("xhr readyState is " + xhr.readyState);
- }
- };
- xhr.setRequestHeader("Content-Type", "application/json");
- xhr.setRequestHeader("Content-Length", data.length);
- xhr.setRequestHeader("Accept", "text/plain");
-
- xhr.send(JSON.stringify(data));
-
- } catch (e) {
- Util.log("Failed to send to server " + e);
- return false;
- }
- return true;
- },
-
- geo : {
- // calculate the distance in km between two GPS locations
- calculateDistance : function (lat1, lon1, lat2, lon2) {
- var dLat = (lat2-lat1) * Math.PI / 180;
- var dLon = (lon2-lon1) * Math.PI / 180;
- var lat1 = lat1 * Math.PI / 180;
- var lat2 = lat2 * Math.PI / 180;
- var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
- Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
- // 6371 km is average earth radius
- return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
- },
-
- calculateDistanceCoordinates : function (events) {
- var result = 0;
- if (events.length > 0) {
- var i = 1;
- while (i < events.length) {
- var position1 = events[i-1].c;
- var position2 = events[i].c;
- result += Util.geo.calculateDistance(position1[1], position1[0], position2[1], position2[0]);
- i++;
- }
- }
- return result;
- },
- // calculate the duration between geolocation coordinates
- calculateDurationCoordinates : function (events) {
- var result = 0;
- if (events.length > 0) {
- var i = 1;
- while (i < events.length) {
- result += (events[i].t - events[i-1].t);
- i++;
- }
- }
- return result;
- }
- }
-};
var getUser = function () {
function dummyuser() {
--- a/src/main/resources/templates/geolocation.ssp Tue Aug 23 18:01:35 2011 -0400
+++ b/src/main/resources/templates/geolocation.ssp Tue Aug 23 18:12:40 2011 -0400
@@ -9,6 +9,8 @@
<title>HTML5 Track</title>
<script src='/scripts/json2.js'>
</script>
+ <script src='/scripts/Utils.js'>
+ </script>
<script src='/scripts/tracker.js'>
</script>
<link rel="stylesheet" href="/styles/tracker.css" type="text/css" />