+
+var geofake = {
+ tracking: false,
+ lastPos: {x: undefined, y: undefined},
+ watchPosition: function(aSuccessCallback, aErrorCallback, aPrefObject) {
+ this.tracking = true;
+ var watchCall = function() {
+ // calc new position in lat/lon degrees
+ // 90° on Earth surface are ~10,000 km at the equator,
+ // so try moving at most 10m at a time
+ if (geofake.lastPos.x)
+ geofake.lastPos.x += (Math.random() - .5) * 90 / 1000000
+ else
+ geofake.lastPos.x = 48.208174
+ if (geofake.lastPos.y)
+ geofake.lastPos.y += (Math.random() - .5) * 90 / 1000000
+ else
+ geofake.lastPos.y = 16.373819
+ aSuccessCallback({timestamp: Date.now(),
+ coords: {latitude: geofake.lastPos.x,
+ longitude: geofake.lastPos.y,
+ accuracy: 20}});
+ if (geofake.tracking)
+ setTimeout(watchCall, 1000);
+ };
+ setTimeout(watchCall, 1000);
+ return "foo";
+ },
+ clearWatch: function(aID) {
+ this.tracking = false;
+ }
+}
+
+function setCentering(aCheckbox) {
+ if (gMapPrefsLoaded && mainDB)
+ gPrefs.set("center_map", aCheckbox.checked);
+ gCenterPosition = aCheckbox.checked;
+}
+
+function setTracking(aCheckbox) {
+ if (gMapPrefsLoaded && mainDB)
+ gPrefs.set("tracking_enabled", aCheckbox.checked);
+ if (aCheckbox.checked)
+ startTracking();
+ else
+ endTracking();
+}
+
+function startTracking() {
+ if (gGeolocation) {
+ gActionLabel.textContent = "Establishing Position";
+ gAction.style.display = "block";
+ gGeoWatchID = gGeolocation.watchPosition(
+ function(position) {
+ if (gActionLabel.textContent) {
+ gActionLabel.textContent = "";
+ gAction.style.display = "none";
+ }
+ // Coords spec: https://developer.mozilla.org/en/XPCOM_Interface_Reference/NsIDOMGeoPositionCoords
+ var tPoint = {time: position.timestamp,
+ coords: {latitude: position.coords.latitude,
+ longitude: position.coords.longitude,
+ altitude: position.coords.altitude,
+ accuracy: position.coords.accuracy,
+ altitudeAccuracy: position.coords.altitudeAccuracy,
+ heading: position.coords.heading,
+ speed: position.coords.speed},
+ beginSegment: !gLastTrackPoint};
+ // Only add point to track is accuracy is good enough.
+ if (tPoint.coords.accuracy < gMinTrackAccuracy) {
+ gLastTrackPoint = tPoint;
+ gTrack.push(tPoint);
+ try { gTrackStore.push(tPoint); } catch(e) {}
+ var redrawn = false;
+ if (gCenterPosition) {
+ var posCoord = gps2xy(position.coords.latitude,
+ position.coords.longitude);
+ if (Math.abs(gPos.x - posCoord.x) > gMapCanvas.width * gZoomFactor / 4 ||
+ Math.abs(gPos.y - posCoord.y) > gMapCanvas.height * gZoomFactor / 4) {
+ gPos.x = posCoord.x;
+ gPos.y = posCoord.y;
+ drawMap(); // This draws the current point as well.
+ redrawn = true;
+ }
+ }
+ if (!redrawn)
+ undrawCurrentLocation();
+ drawTrackPoint(position.coords.latitude, position.coords.longitude, true);
+ }
+ drawCurrentLocation(tPoint);
+ },
+ function(error) {
+ // Ignore erros for the moment, but this is good for debugging.
+ // See https://developer.mozilla.org/en/Using_geolocation#Handling_errors
+ if (gDebug)
+ console.log(error.message);
+ },
+ {enableHighAccuracy: true}
+ );
+ }
+}
+
+function endTracking() {
+ if (gActionLabel.textContent) {
+ gActionLabel.textContent = "";
+ gAction.style.display = "none";
+ }
+ if (gGeoWatchID) {
+ gGeolocation.clearWatch(gGeoWatchID);
+ }
+}
+
+function clearTrack() {
+ gTrack = [];
+ gTrackStore.clear();
+ drawMap();
+}
+
+var gTileService = {
+ objStore: "tilecache",
+
+ get: function(aStyle, aCoords, aCallback) {
+ var norm = normalizeCoords(aCoords);
+ var dbkey = aStyle + "::" + norm.x + "," + norm.y + "," + norm.z;
+ this.getDBCache(dbkey, function(aResult, aEvent) {
+ if (aResult) {
+ // We did get a cached object.
+ // TODO: Look at the timestamp and trigger a reload when it's too old.
+ aCallback(aResult.image, aStyle, aCoords);
+ }
+ else {
+ // Retrieve image from the web and store it in the cache.
+ var XHR = new XMLHttpRequest();
+ XHR.open("GET",
+ gMapStyles[aStyle].url
+ .replace("{x}", norm.x)
+ .replace("{y}", norm.y)
+ .replace("{z}", norm.z)
+ .replace("[a-c]", String.fromCharCode(97 + Math.floor(Math.random() * 2)))
+ .replace("[1-4]", 1 + Math.floor(Math.random() * 3)),
+ true);
+ XHR.responseType = "blob";
+ XHR.addEventListener("load", function () {
+ if (XHR.status === 200) {
+ var blob = XHR.response;
+ gTileService.setDBCache(dbkey, {image: blob, timestamp: Date.now()});
+ aCallback(blob, aStyle, aCoords);
+ }
+ }, false);
+ XHR.send();
+ }
+ });
+ },
+
+ getDBCache: function(aKey, aCallback) {
+ if (!mainDB)
+ return;
+ var transaction = mainDB.transaction([this.objStore]);
+ var request = transaction.objectStore(this.objStore).get(aKey);
+ request.onsuccess = function(event) {
+ aCallback(request.result, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ aCallback(undefined, event);
+ };
+ },
+
+ setDBCache: function(aKey, aValue, aCallback) {
+ if (!mainDB)
+ return;
+ var success = false;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var objStore = transaction.objectStore(this.objStore);
+ var request = objStore.put(aValue, aKey);
+ request.onsuccess = function(event) {
+ success = true;
+ if (aCallback)
+ aCallback(success, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(success, event);
+ };
+ },
+
+ unsetDBCache: function(aKey, aCallback) {
+ if (!mainDB)
+ return;
+ var success = false;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var request = transaction.objectStore(this.objStore).delete(aKey);
+ request.onsuccess = function(event) {
+ success = true;
+ if (aCallback)
+ aCallback(success, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(success, event);
+ }
+ }
+};