From 1842346dd51c9681408f6cfc6fa5768a65ce2e27 Mon Sep 17 00:00:00 2001 From: Hidde Wieringa Date: Sat, 27 Jul 2024 15:14:11 +0200 Subject: [PATCH] implement popup on click --- proxy/js/ui.js | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/proxy/js/ui.js b/proxy/js/ui.js index 0aff5669..4ff9a730 100644 --- a/proxy/js/ui.js +++ b/proxy/js/ui.js @@ -584,8 +584,8 @@ map.on('zoomend', () => onMapZoom(map.getZoom())); // When a click event occurs on a feature in the places layer, open a popup at the // location of the feature, with description HTML from its properties. -map.on('click', 'search', (e) => { - const feature = e.features[0]; +map.on('click', 'search', event => { + const feature = event.features[0]; const coordinates = feature.geometry.coordinates.slice(); const properties = feature.properties; const content = ` @@ -609,12 +609,49 @@ map.on('click', 'search', (e) => { .addTo(map); }); -// Change the cursor to a pointer when the mouse is over the places layer. map.on('mouseenter', 'search', () => { map.getCanvas().style.cursor = 'pointer'; }); - -// Change it back to a pointer when it leaves. map.on('mouseleave', 'search', () => { map.getCanvas().style.cursor = ''; }); + +map.on('mousehover', event => { + const features = map.queryRenderedFeatures(event.point); + if (features.length > 0) { + map.getCanvas().style.cursor = 'pointer'; + } else { + map.getCanvas().style.cursor = ''; + } +}); + +map.on('click', event => { + const features = map.queryRenderedFeatures(event.point); + if (features.length > 0) { + const feature = features[0]; + console.info(event, feature) + const coordinates = feature.geometry === 'Point' + ? feature.geometry.coordinates + : event.lngLat; + const properties = feature.properties; + + const content = ` +
+ ${properties.icon ? `${properties.icon}` : ''} + ${properties.label} + ${icons.edit} +
+
+ ${properties.railway_ref ? `reference: ${properties.railway_ref}` : ''} + ${properties.ref ? `reference: ${properties.ref}` : ''} + ${properties.uic_ref ? `UIC reference: ${properties.uic_ref}` : ''} + ${properties.position ? `position: ${properties.position}` : ''} + ${properties.operator ? `operator: ${properties.operator}` : ''} +
+ `; + new maplibregl.Popup() + .setLngLat(coordinates) + .setHTML(content) + .addTo(map); + } +});