Save selected layer in local storage (#17)
This commit is contained in:
parent
bc7e7888a5
commit
51c1c57e48
21
lib/map.js
21
lib/map.js
|
@ -172,7 +172,7 @@ define(["map/clientlayer", "map/labelslayer",
|
|||
|
||||
var map, userLocation
|
||||
var layerControl
|
||||
var customLayers = new Set()
|
||||
var customLayers = {}
|
||||
var baseLayers = {}
|
||||
|
||||
var locateUserButton = new LocateButton(function (d) {
|
||||
|
@ -259,16 +259,16 @@ define(["map/clientlayer", "map/labelslayer",
|
|||
if (layerName in baseLayers)
|
||||
return
|
||||
|
||||
if (customLayers.has(layerName))
|
||||
if (layerName in customLayers)
|
||||
return
|
||||
|
||||
try {
|
||||
var layer = L.tileLayer.provider(layerName)
|
||||
layerControl.addBaseLayer(layer, layerName)
|
||||
customLayers.add(layerName)
|
||||
customLayers[layerName] = layer
|
||||
|
||||
if (localStorageTest())
|
||||
localStorage.setItem("map/customLayers", JSON.stringify(Array.from(customLayers)))
|
||||
localStorage.setItem("map/customLayers", JSON.stringify(Object.keys(customLayers)))
|
||||
} catch (e) {
|
||||
return
|
||||
}
|
||||
|
@ -324,7 +324,20 @@ define(["map/clientlayer", "map/labelslayer",
|
|||
|
||||
if (d)
|
||||
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})
|
||||
clientLayer.addTo(map)
|
||||
|
|
Loading…
Reference in a new issue