move debug output to console.log; make DB upgrade work more reasonably
[lantea.git] / js / map.js
index ea3b62256c5d54ce23511f004445afc600ef0c73..a6988181b437e3c6af92f91d0dedcf817994d94d 100644 (file)
--- a/js/map.js
+++ b/js/map.js
@@ -3,7 +3,7 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 var gMapCanvas, gMapContext, gTrackCanvas, gTrackContext, gGeolocation;
-var gDebug = true;
+var gDebug = false;
 
 var gTileSize = 256;
 var gMaxZoom = 18; // The minimum is 0.
@@ -122,7 +122,7 @@ function initMap() {
       gWaitCounter++;
       gTrackStore.getList(function(aTPoints) {
         if (gDebug)
-          document.getElementById("debug").textContent = aTPoints.length + " points loaded.";
+          console.log(aTPoints.length + " points loaded.");
         if (aTPoints.length) {
           gTrack = aTPoints;
         }
@@ -133,7 +133,7 @@ function initMap() {
       setTimeout(getPersistentPrefs, 100);
     loopCnt++;
     if (loopCnt > 50) {
-      document.getElementById("debug").textContent = "Loading prefs failed.";
+      console.log("Loading prefs failed.");
     }
   };
   getPersistentPrefs();
@@ -165,13 +165,14 @@ function initMap() {
 function resizeAndDraw() {
   var viewportWidth = Math.min(window.innerWidth, window.outerWidth);
   var viewportHeight = Math.min(window.innerHeight, window.outerHeight);
-
-  gMapCanvas.width = viewportWidth;
-  gMapCanvas.height = viewportHeight;
-  gTrackCanvas.width = viewportWidth;
-  gTrackCanvas.height = viewportHeight;
-  drawMap();
-  showUI();
+  if (gMapCanvas && gTrackCanvas) {
+    gMapCanvas.width = viewportWidth;
+    gMapCanvas.height = viewportHeight;
+    gTrackCanvas.width = viewportWidth;
+    gTrackCanvas.height = viewportHeight;
+    drawMap();
+    showUI();
+  }
 }
 
 function zoomIn() {
@@ -297,12 +298,16 @@ function drawMap() {
           var iyMin = gPos.y - ht / 2;
           var ixoff = Math.round((aCoords.x * size - ixMin) / gZoomFactor);
           var iyoff = Math.round((aCoords.y * size - iyMin) / gZoomFactor);
+          // Would be nice to draw directly from the blob, but that crashes:
+          // gMapContext.drawImage(aImage, ixoff, iyoff);
           var URL = window.URL;
           var imgURL = URL.createObjectURL(aImage);
           var imgObj = new Image();
           imgObj.src = imgURL;
-          gMapContext.drawImage(imgObj, ixoff, iyoff);
-          URL.revokeObjectURL(imgURL);
+          imgObj.onload = function() {
+            gMapContext.drawImage(imgObj, ixoff, iyoff);
+            URL.revokeObjectURL(imgURL);
+          }
         }
       });
     }
@@ -471,10 +476,9 @@ var mapEvHandler = {
                        y: gPos.y + (x - gMapCanvas.height / 2) * gZoomFactor};
         var gpsCoord = xy2gps(ptCoord.x, ptCoord.y);
         var pt2Coord = gps2xy(gpsCoord.latitude, gpsCoord.longitude);
-        document.getElementById("debug").textContent =
-            ptCoord.x + "/" + ptCoord.y + " - " +
-            gpsCoord.latitude + "/" + gpsCoord.longitude + " - " +
-            pt2Coord.x + "/" + pt2Coord.y;
+        console.log(ptCoord.x + "/" + ptCoord.y + " - " +
+                    gpsCoord.latitude + "/" + gpsCoord.longitude + " - " +
+                    pt2Coord.x + "/" + pt2Coord.y);
         */
 
         var newZoomLevel = gPos.z + (delta > 0 ? 1 : -1);
@@ -587,7 +591,8 @@ function startTracking() {
       function(error) {
         // Ignore erros for the moment, but this is good for debugging.
         // See https://developer.mozilla.org/en/Using_geolocation#Handling_errors
-        document.getElementById("debug").textContent = error.message;
+        if (gDebug)
+          console.log(error.message);
       },
       {enableHighAccuracy: true}
     );