diff --git a/server/static/index.html b/server/static/index.html index 556a0c1..11b66ab 100644 --- a/server/static/index.html +++ b/server/static/index.html @@ -46,6 +46,10 @@ + + + + diff --git a/server/static/js/Hashroute.js b/server/static/js/Hashroute.js new file mode 100644 index 0000000..6027be9 --- /dev/null +++ b/server/static/js/Hashroute.js @@ -0,0 +1,59 @@ +'use strict'; + +var Hashroute = { + + setup: function(map, layerMgr){ + function updateHash(){ + var center = map.getCenter(); + location.hash = + layerMgr.getCurrentLayer().id + "/" + + center.lng + "/" + center.lat + "/" + map.getZoom(); + } + + map.on('zoomend', updateHash) + map.on('moveend', updateHash) + updateHash(); + }, + + getLayerId: function(){ + var hashParts = location.hash.substring(1).split("/"); + if (hashParts.length == 4){ + //new format + return +hashParts[0] + + } + + return 0; + }, + + getZoom: function(){ + var hashParts = location.hash.substring(1).split("/"); + if (hashParts.length == 3){ + //old format + return +hashParts[2] + + } else if (hashParts.length == 4){ + //new format + return +hashParts[3] + + } + + return 11; + }, + + getCenter: function(){ + var hashParts = location.hash.substring(1).split("/"); + if (hashParts.length == 3){ + //old format + return [+hashParts[1], +hashParts[0]]; + + } else if (hashParts.length == 4){ + //new format + return [+hashParts[2], +hashParts[1]]; + + } + + return [0, 0]; + } + +}; diff --git a/server/static/js/LayerManager.js b/server/static/js/LayerManager.js index 005c316..4af098f 100644 --- a/server/static/js/LayerManager.js +++ b/server/static/js/LayerManager.js @@ -3,6 +3,7 @@ function LayerManager(layers, map){ this.listeners = []; this.currentLayer = layers[0]; + this.layers = layer; map.on('baselayerchange', function (e) { console.log("baselayerchange", e.layer); @@ -11,6 +12,16 @@ function LayerManager(layers, map){ } +LayerManager.prototype.setLayerId = function(layerId){ + var self = this; + this.layers.forEach(function(layer){ + if (layer.id == layerId){ + self.currentLayer = layer; + } + }); +}, + + LayerManager.prototype.addListener = function(listener){ this.listeners.push(listener); }; diff --git a/server/static/js/main.js b/server/static/js/main.js index 11cbfe6..de6dbc0 100644 --- a/server/static/js/main.js +++ b/server/static/js/main.js @@ -5,14 +5,11 @@ api.getConfig().then(function(cfg){ var wsChannel = new WebSocketChannel(); wsChannel.connect(); - var initialZoom = 11; - var initialCenter = [0, 0]; - var map = L.map('image-map', { minZoom: 2, maxZoom: 12, - center: initialCenter, - zoom: initialZoom, + center: Hashroute.getCenter(), + zoom: Hashroute.getZoom(), crs: SimpleCRS }); @@ -22,6 +19,7 @@ api.getConfig().then(function(cfg){ var overlays = {} var layerMgr = new LayerManager(cfg.layers, map); + layerMgr.setLayerId( Hashroute.getLayerId() ); //All layers cfg.layers.forEach(function(layer){ @@ -39,6 +37,8 @@ api.getConfig().then(function(cfg){ new CoordinatesDisplay({ position: 'bottomleft' }).addTo(map); new WorldInfoDisplay(wsChannel, { position: 'bottomright' }).addTo(map); + Hashroute.setup(map, layerMgr); + }).catch(function(e){ console.error(e); });