Save selected layer in local storage (#17)

This commit is contained in:
Marvin W 2016-04-21 22:32:22 +02:00 committed by PetaByteBoy // Milan Pässler
parent bc7e7888a5
commit 51c1c57e48

View file

@ -172,7 +172,7 @@ define(["map/clientlayer", "map/labelslayer",
var map, userLocation var map, userLocation
var layerControl var layerControl
var customLayers = new Set() var customLayers = {}
var baseLayers = {} var baseLayers = {}
var locateUserButton = new LocateButton(function (d) { var locateUserButton = new LocateButton(function (d) {
@ -259,16 +259,16 @@ define(["map/clientlayer", "map/labelslayer",
if (layerName in baseLayers) if (layerName in baseLayers)
return return
if (customLayers.has(layerName)) if (layerName in customLayers)
return return
try { try {
var layer = L.tileLayer.provider(layerName) var layer = L.tileLayer.provider(layerName)
layerControl.addBaseLayer(layer, layerName) layerControl.addBaseLayer(layer, layerName)
customLayers.add(layerName) customLayers[layerName] = layer
if (localStorageTest()) if (localStorageTest())
localStorage.setItem("map/customLayers", JSON.stringify(Array.from(customLayers))) localStorage.setItem("map/customLayers", JSON.stringify(Object.keys(customLayers)))
} catch (e) { } catch (e) {
return return
} }
@ -324,8 +324,21 @@ define(["map/clientlayer", "map/labelslayer",
if (d) if (d)
d.forEach(addLayer) d.forEach(addLayer)
d = JSON.parse(localStorage.getItem("map/selectedLayer"))
d = d && d.name in baseLayers ? baseLayers[d.name] : d && d.name in customLayers ? customLayers[d.name] : false
if (d) {
map.removeLayer(layers[0].layer)
map.addLayer(d)
}
} }
map.on("baselayerchange", function(e) {
if (localStorageTest())
localStorage.setItem("map/selectedLayer", JSON.stringify({name: e.name}))
})
var clientLayer = new ClientLayer({minZoom: 15, maxZoom: maxLayerZoom}) var clientLayer = new ClientLayer({minZoom: 15, maxZoom: maxLayerZoom})
clientLayer.addTo(map) clientLayer.addTo(map)
clientLayer.setZIndex(5) clientLayer.setZIndex(5)