Skip to content

Commit 29a14a8

Browse files
committed
fix: featurePopup add
1 parent 7a7c8cc commit 29a14a8

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

src/GEOComp.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import styles from "./styles.module.css";
2020
import { i18nObjs, trans } from "./i18n/comps";
2121
import { Geo } from "./vendors";
2222
import { version } from '../package.json';
23-
import { animate, showPopup, getFeatures, setFeatures, clearFeatures, deepMerge, parseFilter } from './vendors/helpers'
23+
import { animate, showPopup, featurePopup, getFeatures, setFeatures, clearFeatures, deepMerge, parseFilter } from './vendors/helpers'
2424
import { useResizeDetector } from "react-resize-detector";
2525
import { featureControl } from './FeaturesControl';
2626
import { geoContext } from './GEOContext';
@@ -461,20 +461,33 @@ GEOComp = withMethodExposing(GEOComp, [
461461
description: "Displays a popup at the specified coordinates with a given message",
462462
params: [
463463
{
464-
name: "coordinates",
465-
type: "JSONValue", // Assuming [longitude, latitude]
466-
description: "Coordinates where the popup should appear",
464+
name: "coordinates/featureEvent",
465+
type: "JSONValue", // Assuming [longitude, latitude] can also be an featureEvent
466+
description: "Coordinates or featureEvent object where the popup should appear",
467467
},
468468
{
469469
name: "message",
470470
type: "string",
471471
description: "Message to display in the popup",
472-
}
472+
},
473+
{
474+
name: "style",
475+
type: "string",
476+
description: "Style of popup (optional)",
477+
},
473478
]
474479
},
475480
execute: async (comp: any, params: any) => {
476481
var map = comp.exposingValues.events['map:init']
477-
if (map) showPopup(map, params[0], params[1]);
482+
if (map) {
483+
switch (params[2]) {
484+
case 'features':
485+
featurePopup(map, params[0], params[1])
486+
break;
487+
default:
488+
showPopup(map, params[0], params[1]);
489+
}
490+
}
478491
}
479492
},
480493
{

src/vendors/Geo.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ function Geo(props) {
671671
// Check for features at the current pointer position
672672
map.forEachFeatureAtPixel(pixel, function (feature, layer) {
673673
// If a layer is found and its selectable property is not false
674-
console.debug('Hover feature on layer:', layer);
674+
//console.debug('Hover feature on layer:', layer);
675675
if (layer && layer.get('selectable') !== false) {
676676
cursorStyle = 'pointer'; // Change the cursor to pointer
677677
return false; // Stop iterating through the features
@@ -712,9 +712,7 @@ function Geo(props) {
712712
loadLayers(map)
713713

714714
//Add map init event
715-
map.once('rendercomplete', () => {
716-
fireEvent('map:init', map)
717-
})
715+
fireEvent('map:init', map)
718716
}
719717
}, [props.features, props.projection, props.startDate, props.endDate, geoRef]);
720718

src/vendors/helpers/Popup.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@
66
import Overlay from 'ol/Overlay';
77
import { getCenter } from 'ol/extent';
88
import { fromLonLat, transformExtent } from 'ol/proj';
9+
import { object } from 'prop-types';
910

1011

11-
/**
12-
* Shows a popup overlay on the provided map at the given coordinates
13-
* with the specified content message. Creates the popup if it does not
14-
* already exist.
15-
*
16-
* @param {ol.Map} map The map to show the popup on.
17-
* @param {ol.Coordinate|ol.Extent} coordinates The popup location as a coordinate pair or extent.
18-
* @param {string} message The HTML content for the popup.
19-
*/
20-
export function showPopup(map, coordinates, message) {
21-
let popup = map.getOverlayById('infoPopup');
12+
function getMapPopup(map, name) {
13+
let popup = map.getOverlayById(name);
2214
if (!popup) {
2315
// Create popup overlay logic
2416
let popupElement = document.createElement('div');
@@ -43,12 +35,28 @@ export function showPopup(map, coordinates, message) {
4335
element: popupElement,
4436
autoPan: true,
4537
autoPanAnimation: { duration: 250 },
46-
id: 'infoPopup'
38+
id: name
4739
});
4840

4941
map.addOverlay(popup);
5042
}
43+
return popup
44+
}
45+
/**
46+
* Shows a popup overlay on the provided map at the given coordinates
47+
* with the specified content message. Creates the popup if it does not
48+
* already exist.
49+
*
50+
* @param {ol.Map} map The map to show the popup on.
51+
* @param {ol.Coordinate|ol.Extent} coordinates The popup location as a coordinate pair or extent.
52+
* @param {string} message The HTML content for the popup.
53+
*/
54+
export function showPopup(map, coordinates, message) {
55+
let popup = getMapPopup(map, 'infoPopup');
5156

57+
if (coordinates && !Array.isArray(coordinates) && coordinates.extent) {
58+
coordinates = coordinates.extent
59+
}
5260
if (Array.isArray(coordinates)) {
5361
// If coordinates have more than two values, assume it's an extent and calculate its center.
5462
if (coordinates.length > 2) {
@@ -63,3 +71,24 @@ export function showPopup(map, coordinates, message) {
6371
popup.setPosition(coordinates);
6472
}
6573
}
74+
75+
76+
/**
77+
* Shows a popup with feature properties in a table.
78+
* Called on map click events. Checks for feature at click location,
79+
* gets its properties, formats them in a table,
80+
* and shows in a popup at the click location.
81+
*
82+
* @param map The map to show the popup on.
83+
* @param event The feature click event
84+
*/
85+
export function featurePopup(map, evtObj, title) {
86+
let tableHTML = '<tr><th>Property</th><th>Value</th></tr>';
87+
// Exclude the geometry property and iterate over the remaining properties
88+
delete evtObj.properties.geometry; // Remove geometry property
89+
for (const [key, value] of Object.entries(evtObj.properties)) {
90+
tableHTML += `<tr><td>${key}</td><td>${value}</td></tr>`;
91+
}
92+
// Now coordinates will always be a pair here, suitable for setPosition.
93+
showPopup(map, evtObj, `${title}<table style="border:1">${tableHTML}</table>`)
94+
}

0 commit comments

Comments
 (0)