6
6
import Overlay from 'ol/Overlay' ;
7
7
import { getCenter } from 'ol/extent' ;
8
8
import { fromLonLat , transformExtent } from 'ol/proj' ;
9
+ import { object } from 'prop-types' ;
9
10
10
11
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 ) ;
22
14
if ( ! popup ) {
23
15
// Create popup overlay logic
24
16
let popupElement = document . createElement ( 'div' ) ;
@@ -43,12 +35,28 @@ export function showPopup(map, coordinates, message) {
43
35
element : popupElement ,
44
36
autoPan : true ,
45
37
autoPanAnimation : { duration : 250 } ,
46
- id : 'infoPopup'
38
+ id : name
47
39
} ) ;
48
40
49
41
map . addOverlay ( popup ) ;
50
42
}
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' ) ;
51
56
57
+ if ( coordinates && ! Array . isArray ( coordinates ) && coordinates . extent ) {
58
+ coordinates = coordinates . extent
59
+ }
52
60
if ( Array . isArray ( coordinates ) ) {
53
61
// If coordinates have more than two values, assume it's an extent and calculate its center.
54
62
if ( coordinates . length > 2 ) {
@@ -63,3 +71,24 @@ export function showPopup(map, coordinates, message) {
63
71
popup . setPosition ( coordinates ) ;
64
72
}
65
73
}
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