1
0
forked from MTSR/mapserver
mapserver/public/js/map/CustomOverlay.js

49 lines
936 B
JavaScript
Raw Normal View History

2019-08-30 12:21:34 +03:00
var customOverlays = {};
try {
2019-09-01 13:06:47 +03:00
customOverlays = JSON.parse(localStorage["mapserver-customOverlays"]);
2019-08-30 12:21:34 +03:00
} catch (e){}
function save(){
localStorage["mapserver-customOverlays"] = JSON.stringify(customOverlays);
}
function onAddLayer(e){
customOverlays[e.name] = true;
save();
}
function onRemoveLayer(e){
customOverlays[e.name] = false;
save();
}
2019-08-30 12:21:34 +03:00
export default function(map, overlays){
Object.keys(customOverlays)
.filter(name => overlays[name])
.forEach(name => {
const layer = overlays[name];
if (customOverlays[name] && !map.hasLayer(layer)){
//Add
map.addLayer(layer);
}
if (!customOverlays[name] && map.hasLayer(layer)){
//Remove
map.removeLayer(layer);
}
});
map.on('unload', () => {
map.off('overlayadd', onAddLayer);
map.off('overlayremove', onRemoveLayer);
2019-08-30 12:21:34 +03:00
});
map.on('overlayadd', onAddLayer);
map.on('overlayremove', onRemoveLayer);
2019-08-30 12:21:34 +03:00
}