Improve readability and create only one style

This commit is contained in:
Andreas Hocevar
2018-03-21 12:01:42 +01:00
committed by GitHub
parent 6e9cf0fd9d
commit 467cf3ce5b

View File

@@ -20,29 +20,16 @@ const vtLayer = new VectorTileLayer({
'ne:ne_10m_admin_0_countries@EPSG%3A900913@pbf/{z}/{x}/{-y}.pbf' 'ne:ne_10m_admin_0_countries@EPSG%3A900913@pbf/{z}/{x}/{-y}.pbf'
}), }),
style: function(feature) { style: function(feature) {
// normal style const selected = !!selection[feature.get(idProp)];
let style = new Style({ return new Style({
stroke: new Stroke({ stroke: new Stroke({
color: 'gray', color: selected ? 'rgba(200,20,20,0.8)' : 'gray',
width: 1 width: selected ? 2 : 1
}), }),
fill: new Fill({ fill: new Fill({
color: 'rgba(20,20,20,0.9)' color: selected ? 'rgba(200,20,20,0.2)' : 'rgba(20,20,20,0.9)'
}) })
}); });
if (selection[feature.get(idProp)]) {
// selection style
style = new Style({
stroke: new Stroke({
color: 'rgba(200,20,20,0.8)',
width: 2
}),
fill: new Fill({
color: 'rgba(200,20,20,0.2)'
})
});
}
return style;
} }
}); });
@@ -59,14 +46,12 @@ const map = new Map({
const selectElement = document.getElementById('type'); const selectElement = document.getElementById('type');
map.on('click', updateSelection); map.on('click', function(event) {
function updateSelection(event) {
const features = map.getFeaturesAtPixel(event.pixel); const features = map.getFeaturesAtPixel(event.pixel);
if (!features) { if (!features) {
selection = {}; selection = {};
// force redraw of layer style // force redraw of layer style
vtLayer.setStyle(vtLayer.getStyleFunction()); vtLayer.setStyle(vtLayer.getStyle());
return; return;
} }
const feature = features[0]; const feature = features[0];
@@ -79,5 +64,5 @@ function updateSelection(event) {
selection[fid] = feature; selection[fid] = feature;
// force redraw of layer style // force redraw of layer style
vtLayer.setStyle(vtLayer.getStyleFunction()); vtLayer.setStyle(vtLayer.getStyle());
} });