Update select features example

This commit is contained in:
Tim Schaub
2022-01-02 15:46:08 -07:00
parent 43e06a7d57
commit 71af0eee43

View File

@@ -1,25 +1,33 @@
import GeoJSON from '../src/ol/format/GeoJSON.js'; import GeoJSON from '../src/ol/format/GeoJSON.js';
import Map from '../src/ol/Map.js'; import Map from '../src/ol/Map.js';
import OSM from '../src/ol/source/OSM.js';
import Select from '../src/ol/interaction/Select.js'; import Select from '../src/ol/interaction/Select.js';
import VectorLayer from '../src/ol/layer/Vector.js';
import VectorSource from '../src/ol/source/Vector.js'; import VectorSource from '../src/ol/source/Vector.js';
import View from '../src/ol/View.js'; import View from '../src/ol/View.js';
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js'; import {Fill, Stroke, Style} from '../src/ol/style.js';
import {altKeyOnly, click, pointerMove} from '../src/ol/events/condition.js'; import {altKeyOnly, click, pointerMove} from '../src/ol/events/condition.js';
const raster = new TileLayer({ const style = new Style({
source: new OSM(), fill: new Fill({
color: '#eeeeee',
}),
}); });
const vector = new VectorLayer({ const vector = new VectorLayer({
source: new VectorSource({ source: new VectorSource({
url: 'data/geojson/countries.geojson', url: 'https://openlayers.org/data/vector/ecoregions.json',
format: new GeoJSON(), format: new GeoJSON(),
}), }),
background: 'white',
style: function (feature) {
const color = feature.get('COLOR') || '#eeeeee';
style.getFill().setColor(color);
return style;
},
}); });
const map = new Map({ const map = new Map({
layers: [raster, vector], layers: [vector],
target: 'map', target: 'map',
view: new View({ view: new View({
center: [0, 0], center: [0, 0],
@@ -29,20 +37,39 @@ const map = new Map({
let select = null; // ref to currently selected interaction let select = null; // ref to currently selected interaction
const selected = new Style({
fill: new Fill({
color: '#eeeeee',
}),
stroke: new Stroke({
color: 'rgba(255, 255, 255, 0.7)',
width: 2,
}),
});
function selectStyle(feature) {
const color = feature.get('COLOR') || '#eeeeee';
selected.getFill().setColor(color);
return selected;
}
// select interaction working on "singleclick" // select interaction working on "singleclick"
const selectSingleClick = new Select(); const selectSingleClick = new Select({style: selectStyle});
// select interaction working on "click" // select interaction working on "click"
const selectClick = new Select({ const selectClick = new Select({
condition: click, condition: click,
style: selectStyle,
}); });
// select interaction working on "pointermove" // select interaction working on "pointermove"
const selectPointerMove = new Select({ const selectPointerMove = new Select({
condition: pointerMove, condition: pointerMove,
style: selectStyle,
}); });
const selectAltClick = new Select({ const selectAltClick = new Select({
style: selectStyle,
condition: function (mapBrowserEvent) { condition: function (mapBrowserEvent) {
return click(mapBrowserEvent) && altKeyOnly(mapBrowserEvent); return click(mapBrowserEvent) && altKeyOnly(mapBrowserEvent);
}, },