Store the state of each overlay layer in localStorage.

This commit is contained in:
Oliver Jowett 2016-08-04 21:18:54 +01:00
parent ca3c4979f5
commit 50fd2ac2af

View file

@ -369,12 +369,48 @@ function initialize_map() {
// Initialize OL3 // Initialize OL3
var layers = createBaseLayers(); var layers = createBaseLayers();
var iconsLayer = new ol.layer.Vector({
name: 'ac_positions',
type: 'overlay',
title: 'Aircraft positions',
source: new ol.source.Vector({
features: PlaneIconFeatures,
})
});
layers.push(new ol.layer.Group({
title: 'Overlays',
layers: [
new ol.layer.Vector({
name: 'site_pos',
type: 'overlay',
title: 'Site position and range rings',
source: new ol.source.Vector({
features: StaticFeatures,
})
}),
new ol.layer.Vector({
name: 'ac_trail',
type: 'overlay',
title: 'Selected aircraft trail',
source: new ol.source.Vector({
features: PlaneTrailFeatures,
})
}),
iconsLayer
]
}));
var foundType = false; var foundType = false;
ol.control.LayerSwitcher.forEachRecursive(layers, function(lyr) { ol.control.LayerSwitcher.forEachRecursive(layers, function(lyr) {
if (lyr.get('type') !== 'base') if (!lyr.get('name'))
return; return;
if (lyr.get('type') === 'base') {
if (MapType === lyr.get('name')) { if (MapType === lyr.get('name')) {
foundType = true; foundType = true;
lyr.setVisible(true); lyr.setVisible(true);
@ -387,6 +423,17 @@ function initialize_map() {
MapType = localStorage['MapType'] = evt.target.get('name'); MapType = localStorage['MapType'] = evt.target.get('name');
} }
}); });
} else if (lyr.get('type') === 'overlay') {
var visible = localStorage['layer_' + lyr.get('name')];
if (visible != undefined) {
// javascript, why must you taunt me with gratuitous type problems
lyr.setVisible(visible === "true");
}
lyr.on('change:visible', function(evt) {
localStorage['layer_' + evt.target.get('name')] = evt.target.getVisible();
});
}
}) })
if (!foundType) { if (!foundType) {
@ -400,34 +447,6 @@ function initialize_map() {
}); });
} }
var iconsLayer = new ol.layer.Vector({
title: 'Aircraft positions',
source: new ol.source.Vector({
features: PlaneIconFeatures,
})
});
layers.push(new ol.layer.Group({
title: 'Overlays',
layers: [
new ol.layer.Vector({
title: 'Site position and range rings',
source: new ol.source.Vector({
features: StaticFeatures,
})
}),
new ol.layer.Vector({
title: 'Selected aircraft trail',
source: new ol.source.Vector({
features: PlaneTrailFeatures,
})
}),
iconsLayer
]
}));
OLMap = new ol.Map({ OLMap = new ol.Map({
target: 'map_canvas', target: 'map_canvas',
layers: layers, layers: layers,