create hackish way to save GPX tracks and make the map only shift when we get enough...
authorRobert Kaiser <kairo@kairo.at>
Mon, 19 Dec 2011 01:00:05 +0000 (02:00 +0100)
committerRobert Kaiser <kairo@kairo.at>
Mon, 19 Dec 2011 01:00:05 +0000 (02:00 +0100)
index.html
js/map.js
js/ui.js

index 8c889fc..2b4dd13 100644 (file)
@@ -54,6 +54,8 @@
 <span id="zoomLevel">Z</span>
 <input type="button" id="zoomOutButton" value="-"
        onclick="zoomOut();"><br/>
+<input type="button" id="saveButton" value="Save Track"
+       onclick="saveTrack();"><br/>
 <input type="button" id="settingsButton" value="Settings"
        onclick="toggleSettings();">
 <fieldset id="settings"><legend>Settings</legend>
index 1ce6226..4cd4ea7 100644 (file)
--- a/js/map.js
+++ b/js/map.js
@@ -418,9 +418,12 @@ function startTracking() {
         drawTrackPoint(position.coords.latitude, position.coords.longitude);
         if (gCenterPosition) {
           var posCoord = gps2xy(position.coords.latitude, position.coords.longitude);
-          gPos.x = posCoord.x;
-          gPos.y = posCoord.y;
-          drawMap();
+          if (Math.abs(gPos.x - posCoord.x) > gCanvas.width * gZoomFactor / 4 ||
+              Math.abs(gPos.y - posCoord.y) > gCanvas.height * gZoomFactor / 4) {
+            gPos.x = posCoord.x;
+            gPos.y = posCoord.y;
+            drawMap();
+          }
         }
       },
       function(error) {
index 0171e27..187a84d 100644 (file)
--- a/js/ui.js
+++ b/js/ui.js
@@ -62,3 +62,38 @@ function toggleSettings() {
     fs.style.display = "none";
   }
 }
+
+function makeISOString(aTimestamp) {
+  // ISO time format is YYYY-MM-DDTHH:mm:ssZ
+  var tsDate = new Date(aTimestamp);
+  return tsDate.getUTCFullYear() + "-" +
+         (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
+         (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
+         (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
+         (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
+         (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
+}
+
+function saveTrack() {
+  if (gTrack.length) {
+    var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
+    out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
+    out += '  <trk>' + "\n";
+    out += '    <trkseg>' + "\n";
+    for (var i = 0; i < gTrack.length; i++) {
+      out += '      <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
+                                    gTrack[i].coords.longitude + '">' + "\n";
+      if (gTrack[i].coords.altitude) {
+        out += '        <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
+      }
+      out += '        <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
+      out += '      </trkpt>' + "\n";
+      gTrack[i].coords.latitude, gTrack[i].coords.longitude;
+    }
+    out += '    </trkseg>' + "\n";
+    out += '  </trk>' + "\n";
+    out += '</gpx>' + "\n";
+    var outDataURI = "data:application/octet-stream," + encodeURIComponent(out);
+    window.open(outDataURI, 'GPX Track');
+  }
+}