Update hit detection example
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
---
|
---
|
||||||
layout: example.html
|
layout: example.html
|
||||||
title: Vector Layer Hit Detection
|
title: Vector Layer Hit Detection
|
||||||
shortdesc: Example of hit detection on a countries vector layer with country information.
|
shortdesc: Example of hit detection on an ecoregions vector layer with protection status.
|
||||||
docs: >
|
docs: >
|
||||||
The countries are loaded from a GeoJSON file. Information about countries is
|
The ecoregions are loaded from a GeoJSON file. Information about features is
|
||||||
on hover and click is retrieved using the layer's `getFeatures()` method. For
|
retrieved using the layer's `getFeatures()` method on hover and click. For
|
||||||
vector layers, this function resolves with an array of only the topmost
|
vector layers, this function resolves with an array of only the topmost
|
||||||
feature. It uses a very efficient hit detection algorithm, at the cost of
|
feature. It uses an efficient hit detection algorithm, at the cost of
|
||||||
accuracy. For pixel exact hit detection, when performance is not a concern,
|
accuracy. For pixel exact hit detection, when performance is not a concern,
|
||||||
use the map's `getFeaturesAtPixel()` or `forEachFeatureAtPixel()` methods.
|
use the map's `getFeaturesAtPixel()` or `forEachFeatureAtPixel()` methods.
|
||||||
tags: "vector, geojson, click, hover, hit detection"
|
tags: "vector, geojson, click, hover, hit detection"
|
||||||
|
|||||||
@@ -3,35 +3,23 @@ import Map from '../src/ol/Map.js';
|
|||||||
import VectorLayer from '../src/ol/layer/Vector.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 {Fill, Stroke, Style, Text} from '../src/ol/style.js';
|
import {Fill, Stroke, Style} from '../src/ol/style.js';
|
||||||
|
|
||||||
const style = new Style({
|
const style = new Style({
|
||||||
fill: new Fill({
|
fill: new Fill({
|
||||||
color: 'rgba(255, 255, 255, 0.6)',
|
color: '#eeeeee',
|
||||||
}),
|
|
||||||
stroke: new Stroke({
|
|
||||||
color: '#319FD3',
|
|
||||||
width: 1,
|
|
||||||
}),
|
|
||||||
text: new Text({
|
|
||||||
font: '12px Calibri,sans-serif',
|
|
||||||
fill: new Fill({
|
|
||||||
color: '#000',
|
|
||||||
}),
|
|
||||||
stroke: new Stroke({
|
|
||||||
color: '#fff',
|
|
||||||
width: 3,
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const vectorLayer = new VectorLayer({
|
const vectorLayer = new VectorLayer({
|
||||||
|
background: '#1a2b39',
|
||||||
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(),
|
||||||
}),
|
}),
|
||||||
style: function (feature) {
|
style: function (feature) {
|
||||||
style.getText().setText(feature.get('name'));
|
const color = feature.get('COLOR_NNH') || '#eeeeee';
|
||||||
|
style.getFill().setColor(color);
|
||||||
return style;
|
return style;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -47,31 +35,15 @@ const map = new Map({
|
|||||||
|
|
||||||
const highlightStyle = new Style({
|
const highlightStyle = new Style({
|
||||||
stroke: new Stroke({
|
stroke: new Stroke({
|
||||||
color: '#f00',
|
color: 'rgba(255, 255, 255, 0.7)',
|
||||||
width: 1,
|
width: 2,
|
||||||
}),
|
|
||||||
fill: new Fill({
|
|
||||||
color: 'rgba(255,0,0,0.1)',
|
|
||||||
}),
|
|
||||||
text: new Text({
|
|
||||||
font: '12px Calibri,sans-serif',
|
|
||||||
fill: new Fill({
|
|
||||||
color: '#000',
|
|
||||||
}),
|
|
||||||
stroke: new Stroke({
|
|
||||||
color: '#f00',
|
|
||||||
width: 3,
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const featureOverlay = new VectorLayer({
|
const featureOverlay = new VectorLayer({
|
||||||
source: new VectorSource(),
|
source: new VectorSource(),
|
||||||
map: map,
|
map: map,
|
||||||
style: function (feature) {
|
style: highlightStyle,
|
||||||
highlightStyle.getText().setText(feature.get('name'));
|
|
||||||
return highlightStyle;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let highlight;
|
let highlight;
|
||||||
@@ -80,7 +52,7 @@ const displayFeatureInfo = function (pixel) {
|
|||||||
const feature = features.length ? features[0] : undefined;
|
const feature = features.length ? features[0] : undefined;
|
||||||
const info = document.getElementById('info');
|
const info = document.getElementById('info');
|
||||||
if (features.length) {
|
if (features.length) {
|
||||||
info.innerHTML = feature.getId() + ': ' + feature.get('name');
|
info.innerHTML = feature.get('ECO_NAME') + ': ' + feature.get('NNH_NAME');
|
||||||
} else {
|
} else {
|
||||||
info.innerHTML = ' ';
|
info.innerHTML = ' ';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user