83 lines
1.9 KiB
JavaScript
83 lines
1.9 KiB
JavaScript
import GeoJSON from '../src/ol/format/GeoJSON.js';
|
|
import Map from '../src/ol/Map.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 {Fill, Stroke, Style} from '../src/ol/style.js';
|
|
|
|
const style = new Style({
|
|
fill: new Fill({
|
|
color: '#eeeeee',
|
|
}),
|
|
});
|
|
|
|
const vectorLayer = new VectorLayer({
|
|
background: '#1a2b39',
|
|
source: new VectorSource({
|
|
url: 'https://openlayers.org/data/vector/ecoregions.json',
|
|
format: new GeoJSON(),
|
|
}),
|
|
style: function (feature) {
|
|
const color = feature.get('COLOR_NNH') || '#eeeeee';
|
|
style.getFill().setColor(color);
|
|
return style;
|
|
},
|
|
});
|
|
|
|
const map = new Map({
|
|
layers: [vectorLayer],
|
|
target: 'map',
|
|
view: new View({
|
|
center: [0, 0],
|
|
zoom: 1,
|
|
}),
|
|
});
|
|
|
|
const highlightStyle = new Style({
|
|
stroke: new Stroke({
|
|
color: 'rgba(255, 255, 255, 0.7)',
|
|
width: 2,
|
|
}),
|
|
});
|
|
|
|
const featureOverlay = new VectorLayer({
|
|
source: new VectorSource(),
|
|
map: map,
|
|
style: highlightStyle,
|
|
});
|
|
|
|
let highlight;
|
|
const displayFeatureInfo = function (pixel) {
|
|
vectorLayer.getFeatures(pixel).then(function (features) {
|
|
const feature = features.length ? features[0] : undefined;
|
|
const info = document.getElementById('info');
|
|
if (features.length) {
|
|
info.innerHTML = feature.get('ECO_NAME') + ': ' + feature.get('NNH_NAME');
|
|
} else {
|
|
info.innerHTML = ' ';
|
|
}
|
|
|
|
if (feature !== highlight) {
|
|
if (highlight) {
|
|
featureOverlay.getSource().removeFeature(highlight);
|
|
}
|
|
if (feature) {
|
|
featureOverlay.getSource().addFeature(feature);
|
|
}
|
|
highlight = feature;
|
|
}
|
|
});
|
|
};
|
|
|
|
map.on('pointermove', function (evt) {
|
|
if (evt.dragging) {
|
|
return;
|
|
}
|
|
const pixel = map.getEventPixel(evt.originalEvent);
|
|
displayFeatureInfo(pixel);
|
|
});
|
|
|
|
map.on('click', function (evt) {
|
|
displayFeatureInfo(evt.pixel);
|
|
});
|