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

@@ -7,4 +7,4 @@ docs: >
tags: "select, vector" tags: "select, vector"
--- ---
<div id="map" class="map"></div> <div id="map" class="map"></div>
<span id="status"></span> <span id="status">&nbsp;</span>

View File

@@ -1,36 +1,33 @@
import Fill from '../src/ol/style/Fill.js'; import Fill from '../src/ol/style/Fill.js';
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 Stroke from '../src/ol/style/Stroke.js'; import Stroke from '../src/ol/style/Stroke.js';
import Style from '../src/ol/style/Style.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 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';
const raster = new TileLayer({ const style = new Style({
source: new OSM(),
});
const highlightStyle = new Style({
fill: new Fill({ fill: new Fill({
color: 'rgba(255,255,255,0.7)', color: '#eeeeee',
}),
stroke: new Stroke({
color: '#3399CC',
width: 3,
}), }),
}); });
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],
@@ -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'); const status = document.getElementById('status');
let selected = null;
map.on('pointermove', function (e) { map.on('pointermove', function (e) {
if (selected !== null) { if (selected !== null) {
selected.setStyle(undefined); selected.setStyle(undefined);
@@ -49,12 +56,13 @@ map.on('pointermove', function (e) {
map.forEachFeatureAtPixel(e.pixel, function (f) { map.forEachFeatureAtPixel(e.pixel, function (f) {
selected = f; selected = f;
f.setStyle(highlightStyle); selectStyle.getFill().setColor(f.get('COLOR') || '#eeeeee');
f.setStyle(selectStyle);
return true; return true;
}); });
if (selected) { if (selected) {
status.innerHTML = '&nbsp;Hovering: ' + selected.get('name'); status.innerHTML = selected.get('ECO_NAME');
} else { } else {
status.innerHTML = '&nbsp;'; status.innerHTML = '&nbsp;';
} }