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

@ -366,27 +366,74 @@ function initialize_map() {
document.getElementById("infoblock_country").style.display = 'none'; // hide country row document.getElementById("infoblock_country").style.display = 'none'; // hide country row
} }
// 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 (MapType === lyr.get('name')) { if (lyr.get('type') === 'base') {
foundType = true; if (MapType === lyr.get('name')) {
lyr.setVisible(true); foundType = true;
} else { lyr.setVisible(true);
lyr.setVisible(false); } else {
} lyr.setVisible(false);
lyr.on('change:visible', function(evt) {
if (evt.target.getVisible()) {
MapType = localStorage['MapType'] = evt.target.get('name');
} }
});
lyr.on('change:visible', function(evt) {
if (evt.target.getVisible()) {
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,35 +447,7 @@ function initialize_map() {
}); });
} }
var iconsLayer = new ol.layer.Vector({ OLMap = new ol.Map({
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({
target: 'map_canvas', target: 'map_canvas',
layers: layers, layers: layers,
view: new ol.View({ view: new ol.View({