diff --git a/src/components/utilities/map/js/dynamic.js b/src/components/utilities/map/js/dynamic.js index 8cc338d0..96cf95c2 100644 --- a/src/components/utilities/map/js/dynamic.js +++ b/src/components/utilities/map/js/dynamic.js @@ -153,6 +153,11 @@ maps.forEach(container => { }) }) + const titlePopup = new System.Popup({ + ...popupObject, + closeOnClick: false + }) + mapElement.on('click', 'woodlands-map-detail', (event) => { const coordinates = event.features[0].geometry.coordinates.slice() const html = event.features[0].properties.html @@ -168,6 +173,7 @@ maps.forEach(container => { coordinates[0] += event.lngLat.lng > coordinates[0] ? 360 : -360 } + titlePopup.remove() new System.Popup(popupObject) .setLngLat(coordinates) .setHTML(html) @@ -182,12 +188,28 @@ maps.forEach(container => { mapElement.getCanvas().style.cursor = '' }) - mapElement.on('mouseenter', 'woodlands-map-detail', () => { + mapElement.on('mouseenter', 'woodlands-map-detail', (event) => { + const coordinates = event.features[0].geometry.coordinates.slice() + const description = event.features[0].properties.title + mapElement.getCanvas().style.cursor = 'pointer' + + // Ensure that if the map is zoomed out such that + // multiple copies of the feature are visible, the + // popup appears over the copy being pointed to. + while (Math.abs(event.lngLat.lng - coordinates[0]) > 180) { + coordinates[0] += event.lngLat.lng > coordinates[0] ? 360 : -360 + } + + titlePopup + .setLngLat(coordinates) + .setHTML(description) + .addTo(mapElement) }) mapElement.on('mouseleave', 'woodlands-map-detail', () => { mapElement.getCanvas().style.cursor = '' + titlePopup.remove() }) const countPopup = new System.Popup({