add an upload button for OSM track upload - still doesn't work right, unfortunately
authorRobert Kaiser <kairo@kairo.at>
Fri, 12 Apr 2013 22:32:38 +0000 (00:32 +0200)
committerRobert Kaiser <kairo@kairo.at>
Fri, 12 Apr 2013 22:32:38 +0000 (00:32 +0200)
index.html
js/map.js
js/ui.js
style/lantea.css

index 1f95b6e77679c5c6ce02320f8b68694740baefd3..24de1eeed64458a4a910ff02e01bd840b435fb6e 100644 (file)
@@ -26,6 +26,8 @@
        onclick="saveTrack();">
 <input type="button" id="dumpTrackButton" value="Dump"
        onclick="saveTrackDump();" class="debugHide">
+<input type="button" id="uploadTrackButton" value="Upload" disabled="true"
+       onclick="uploadTrack();">
 <input type="button" id="clearTrackButton" value="Clear"
        onclick="clearTrack();"><br/>
 <input type="checkbox" id="trackCheckbox"
 Map style:
 <select id="mapSelector" onchange="setMapStyle();">
 <!-- option value="osm_mapnik">OpenStreetMap (Mapnik)</option -->
-</select><br/>
+</select>
+<br/>
 <input type="button" id="clearCacheButton" value="Clear Cached Maps"
        onclick="gTileService.clearDB();">
+<p class="settingsSubTitle">OpenStreetMap Track Upload:</p>
+<label for="uploadUser">User name:</label>
+<input type="text" id="uploadUser" maxlength="255"
+       onchange="setUploadField(this);"><br/>
+<label for="uploadPwd">Password:</label>
+<input type="password" id="uploadPwd" maxlength="255"
+       onchange="setUploadField(this);">
 </fieldset>
 </div>
 
index a3a459b5675a9afab3d44a0e454fd6e71b6e7dc9..d269a9574aec018f21894c08aeb674341852a3e5 100644 (file)
--- a/js/map.js
+++ b/js/map.js
@@ -427,6 +427,10 @@ var mapEvHandler = {
   handleEvent: function(aEvent) {
     var touchEvent = aEvent.type.indexOf('touch') != -1;
 
+    // Bail out if the event is happening on an input.
+    if (aEvent.target.tagName.toLowerCase() == "input")
+      return;
+
     // Bail out on unwanted map moves, but not zoom or keyboard events.
     if (aEvent.type.indexOf("mouse") === 0 || aEvent.type.indexOf("touch") === 0) {
       // Bail out if this is neither a touch nor left-click.
index b9c0519d0fe2740f7b6cfc4c970967997f3c407a..f8a45b1d6278ce230a6fb58b261951d6726bb09e 100644 (file)
--- a/js/ui.js
+++ b/js/ui.js
@@ -9,6 +9,7 @@ var mainDB;
 var gUIHideCountdown = 0;
 var gWaitCounter = 0;
 var gAction, gActionLabel;
+var gOSMAPIURL = "http://api06.dev.openstreetmap.org/"; // "http://api.openstreetmap.org/";
 
 window.onload = function() {
   gAction = document.getElementById("action");
@@ -60,6 +61,17 @@ window.onload = function() {
       gActionLabel.textContent = "";
       gAction.style.display = "none";
       setTracking(document.getElementById("trackCheckbox"));
+      gPrefs.get("osm_user", function(aValue) {
+        if (aValue) {
+          document.getElementById("uploadUser").value = aValue;
+          document.getElementById("uploadTrackButton").disabled = false;
+        }
+      });
+      gPrefs.get("osm_pwd", function(aValue) {
+        var upwd = document.getElementById("uploadPwd");
+        if (aValue)
+          document.getElementById("uploadPwd").value = aValue;
+      });
     }
     else
       setTimeout(waitForInitAndDraw, 100);
@@ -197,6 +209,18 @@ var uiEvHandler = {
   }
 };
 
+function setUploadField(aField) {
+  switch (aField.id) {
+    case "uploadUser":
+      gPrefs.set("osm_user", aField.value);
+      document.getElementById("uploadTrackButton").disabled = !aField.value.length;
+      break;
+    case "uploadPwd":
+      gPrefs.set("osm_pwd", aField.value);
+      break;
+  }
+}
+
 function makeISOString(aTimestamp) {
   // ISO time format is YYYY-MM-DDTHH:mm:ssZ
   var tsDate = new Date(aTimestamp);
@@ -208,41 +232,96 @@ function makeISOString(aTimestamp) {
          (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++) {
-      if (gTrack[i].beginSegment && i > 0) {
-        out += '    </trkseg>' + "\n";
+function convertTrack(aTargetFormat) {
+  var out = "";
+  switch (aTargetFormat) {
+    case "gpx":
+      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";
+      if (gTrack.length) {
+        out += '  <trk>' + "\n";
         out += '    <trkseg>' + "\n";
+        for (var i = 0; i < gTrack.length; i++) {
+          if (gTrack[i].beginSegment && i > 0) {
+            out += '    </trkseg>' + "\n";
+            out += '    <trkseg>' + "\n";
+          }
+          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";
+        }
+        out += '    </trkseg>' + "\n";
+        out += '  </trk>' + "\n";
       }
-      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";
-    }
-    out += '    </trkseg>' + "\n";
-    out += '  </trk>' + "\n";
-    out += '</gpx>' + "\n";
-    var outDataURI = "data:application/gpx+xml," + encodeURIComponent(out);
+      out += '</gpx>' + "\n";
+      break;
+    case "json":
+      out = JSON.stringify(gTrack);
+      break;
+    default:
+      break;
+  }
+  return out;
+}
+
+function saveTrack() {
+  if (gTrack.length) {
+    var outDataURI = "data:application/gpx+xml," +
+                     encodeURIComponent(convertTrack("gpx"));
     window.open(outDataURI, 'GPX Track');
   }
 }
 
 function saveTrackDump() {
   if (gTrack.length) {
-    var out = JSON.stringify(gTrack);
-    var outDataURI = "data:application/json," + encodeURIComponent(out);
+    var outDataURI = "data:application/json," +
+                     encodeURIComponent(convertTrack("json"));
     window.open(outDataURI, 'JSON dump');
   }
 }
 
+function uploadTrack() {
+  // See http://wiki.openstreetmap.org/wiki/Api06#Uploading_traces
+  var trackBlob = new Blob([convertTrack("gpx")], { "type" : "application/gpx+xml" });
+  var formData = new FormData();
+  formData.append("file", trackBlob);
+  formData.append("description", "Track recorded via Lantea Maps");
+  //formData.append("tags", "");
+  formData.append("visibility", "private");
+  var XHR = new XMLHttpRequest();
+  XHR.onreadystatechange = function() {
+    if (XHR.readyState == 4) {/*
+      gLog.appendChild(document.createElement("li"))
+          .appendChild(document.createTextNode(aURL + " - " + XHR.status +
+                                               " " + XHR.statusText));*/
+    }
+    if (XHR.readyState == 4 && XHR.status == 200) {
+      // so far so good
+      reportUploadStatus(true);
+    } else if (XHR.readyState == 4 && XHR.status != 200) {
+      // fetched the wrong page or network error...
+      reportUploadStatus(false);
+    }
+  };
+  XHR.open("POST", gOSMAPIURL + "api/0.6/gpx/create", true,
+           document.getElementById("uploadUser").value,
+           document.getElementById("uploadPwd").value);
+  try {
+    XHR.send(formData);
+  }
+  catch (e) {
+    reportUploadStatus(false, e);
+  }
+}
+
+function reportUploadStatus(aSuccess, aMessage) {
+  alert(aMessage ? aMessage : (aSuccess ? "success" : "failure"));
+}
+
 var gPrefs = {
   objStore: "prefs",
 
index 3d2aa8cc1c333299adf14e5a647d2881aebca3f3..fd4dd225167c8d78117b01680c58548aedf852d5 100644 (file)
@@ -12,31 +12,9 @@ h1 {
   display: none;
 }
 
-#menuArea {
-  position: absolute;
-  /* width: 30em; */
-  left: 1%;
-  top: 1em;
-  z-index: 5;
-}
-
-#zoomArea {
-  position: absolute;
-  right: 1%;
-  top: 1em;
-  z-index: 5;
-  text-align: center;
-}
-
-#fullscreenArea {
+.overlayArea {
   position: absolute;
-  right: 1%;
-  bottom: 2em;
   z-index: 5;
-  text-align: center;
-}
-
-.overlayArea {
   transition-property: opacity;
   transition-duration: .2s;
 }
@@ -55,6 +33,24 @@ h1 {
   font-size: 3mozmm;
 }
 
+#menuArea {
+  /* width: 30em; */
+  left: 1%;
+  top: 1em;
+}
+
+#zoomArea {
+  right: 1%;
+  top: 1em;
+  text-align: center;
+}
+
+#fullscreenArea {
+  right: 1%;
+  bottom: 2em;
+  text-align: center;
+}
+
 #zoomLevel {
   background-color: rgba(255, 255, 255, .8);
   border-radius: 3px;
@@ -105,8 +101,12 @@ h1 {
   display: none;
 }
 
+.settingsSubTitle {
+  margin: .5em 0 0;
+  font-weight: bold;
+}
+
 #copyright {
-  position: absolute;
   bottom: 5px;
   right: .5em;
   margin: 0;