diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-04 22:01:14 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-04 22:01:14 +0100 |
commit | 02808dcf9b905d4ada07c10c1032cfbb7ca237e1 (patch) | |
tree | e726e0add138ba141a64f98fec51ee8321935434 | |
parent | 82acbeacb1f57f58383be4a2210138d40c6a1d6f (diff) | |
download | MinedMap-02808dcf9b905d4ada07c10c1032cfbb7ca237e1.tar MinedMap-02808dcf9b905d4ada07c10c1032cfbb7ca237e1.zip |
viewer: more URL hash improvements
-rw-r--r-- | viewer/MinedMap.js | 75 |
1 files changed, 40 insertions, 35 deletions
diff --git a/viewer/MinedMap.js b/viewer/MinedMap.js index 7960d96..062d0d0 100644 --- a/viewer/MinedMap.js +++ b/viewer/MinedMap.js @@ -122,18 +122,25 @@ window.createMap = function () { mipmaps = res.mipmaps, spawn = res.spawn; - var args = parseHash(), - zoom = parseInt(args['zoom']), - x = parseFloat(args['x']), - z = parseFloat(args['z']), - light = parseInt(args['light']); - - if (isNaN(zoom)) - zoom = 0; - if (isNaN(x)) - x = spawn.x; - if (isNaN(z)) - z = spawn.z; + var x, z, zoom, light; + + var updateParams = function () { + var args = parseHash(); + + zoom = parseInt(args['zoom']); + x = parseFloat(args['x']); + z = parseFloat(args['z']); + light = parseInt(args['light']); + + if (isNaN(zoom)) + zoom = 0; + if (isNaN(x)) + x = spawn.x; + if (isNaN(z)) + z = spawn.z; + }; + + updateParams(); var map = L.map('map', { center: [-z, x], @@ -169,14 +176,13 @@ window.createMap = function () { }); var makeHash = function () { - var zoom = map.getZoom(), - center = map.getCenter(), - x = Math.round(center.lng), - z = Math.round(-center.lat), - ret = '#x='+x+'&z='+z+'&zoom='+zoom; + var ret = '#x='+x+'&z='+z; + + if (zoom != 0) + ret += '&zoom='+zoom; if (map.hasLayer(lightLayer)) - ret += '&light=1'; + ret += '&light=1'; return ret; }; @@ -185,30 +191,27 @@ window.createMap = function () { window.location.hash = makeHash(); }; + var refreshHash = function () { + zoom = map.getZoom(); + center = map.getCenter(); + x = Math.round(center.lng); + z = Math.round(-center.lat); + + updateHash(); + } + updateHash(); - map.on('moveend', updateHash); - map.on('zoomend', updateHash); - map.on('layeradd', updateHash); - map.on('layerremove', updateHash); + map.on('moveend', refreshHash); + map.on('zoomend', refreshHash); + map.on('layeradd', refreshHash); + map.on('layerremove', refreshHash); window.onhashchange = function () { if (window.location.hash == makeHash()) return; - var args = parseHash(), - center = map.getCenter(), - zoom = parseInt(args['zoom']), - x = parseFloat(args['x']), - z = parseFloat(args['z']), - light = parseInt(args['light']); - - if (isNaN(zoom)) - zoom = map.getZoom(); - if (isNaN(x)) - x = center.lng; - if (isNaN(z)) - z = -center.lat; + updateParams(); map.setView([-z, x], zoom); @@ -216,6 +219,8 @@ window.createMap = function () { map.addLayer(lightLayer); else map.removeLayer(lightLayer); + + updateHash(); }; }; |