Update select on hover example

This commit is contained in:
Tim Schaub
2022-01-07 13:00:35 -07:00
parent 73f54c1e6c
commit eed400ca1c
2 changed files with 26 additions and 18 deletions

View File

@@ -1,36 +1,33 @@
import Fill from '../src/ol/style/Fill.js';
import GeoJSON from '../src/ol/format/GeoJSON.js';
import Map from '../src/ol/Map.js';
import OSM from '../src/ol/source/OSM.js';
import Stroke from '../src/ol/style/Stroke.js';
import Style from '../src/ol/style/Style.js';
import VectorLayer from '../src/ol/layer/Vector.js';
import VectorSource from '../src/ol/source/Vector.js';
import View from '../src/ol/View.js';
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
const raster = new TileLayer({
source: new OSM(),
});
const highlightStyle = new Style({
const style = new Style({
fill: new Fill({
color: 'rgba(255,255,255,0.7)',
}),
stroke: new Stroke({
color: '#3399CC',
width: 3,
color: '#eeeeee',
}),
});
const vector = new VectorLayer({
source: new VectorSource({
url: 'data/geojson/countries.geojson',
url: 'https://openlayers.org/data/vector/ecoregions.json',
format: new GeoJSON(),
}),
background: 'white',
style: function (feature) {
const color = feature.get('COLOR') || '#eeeeee';
style.getFill().setColor(color);
return style;
},
});
const map = new Map({
layers: [raster, vector],
layers: [vector],
target: 'map',
view: new View({
center: [0, 0],
@@ -38,9 +35,19 @@ const map = new Map({
}),
});
let selected = null;
const selectStyle = new Style({
fill: new Fill({
color: '#eeeeee',
}),
stroke: new Stroke({
color: 'rgba(255, 255, 255, 0.7)',
width: 2,
}),
});
const status = document.getElementById('status');
let selected = null;
map.on('pointermove', function (e) {
if (selected !== null) {
selected.setStyle(undefined);
@@ -49,12 +56,13 @@ map.on('pointermove', function (e) {
map.forEachFeatureAtPixel(e.pixel, function (f) {
selected = f;
f.setStyle(highlightStyle);
selectStyle.getFill().setColor(f.get('COLOR') || '#eeeeee');
f.setStyle(selectStyle);
return true;
});
if (selected) {
status.innerHTML = ' Hovering: ' + selected.get('name');
status.innerHTML = selected.get('ECO_NAME');
} else {
status.innerHTML = ' ';
}