diff --git a/examples/select-hover-features.html b/examples/select-hover-features.html index 5d2f0fa693..670e6156bc 100644 --- a/examples/select-hover-features.html +++ b/examples/select-hover-features.html @@ -7,4 +7,4 @@ docs: > tags: "select, vector" ---
- +  diff --git a/examples/select-hover-features.js b/examples/select-hover-features.js index 4244481382..00bf8aa31c 100644 --- a/examples/select-hover-features.js +++ b/examples/select-hover-features.js @@ -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 = ' '; }