Improve icon-sprite-webgl example to include hit detection

This commit is contained in:
Olivier Guyot
2019-06-03 09:53:14 +02:00
parent 5ffca0633c
commit 917950a32b
2 changed files with 17 additions and 1 deletions

View File

@@ -18,3 +18,4 @@ cloak:
value: Your Mapbox access token from https://mapbox.com/ here
---
<div id="map" class="map"></div>
<div>Current sighting: <span id="info"></span></div>

View File

@@ -103,7 +103,7 @@ function loadData() {
loadData();
new Map({
const map = new Map({
layers: [
new TileLayer({
source: new TileJSON({
@@ -121,3 +121,18 @@ new Map({
zoom: 2
})
});
const info = document.getElementById('info');
map.on('pointermove', function(evt) {
if (map.getView().getInteracting()) {
return;
}
const pixel = evt.pixel;
info.innerText = '';
map.forEachFeatureAtPixel(pixel, function(feature) {
const datetime = feature.get('datetime');
const duration = feature.get('duration');
const shape = feature.get('shape');
info.innerText = 'On ' + datetime + ', lasted ' + duration + ' seconds and had a "' + shape + '" shape.';
});
});