From ee643c14da46ae0e2fa8c99c1c39e3507186f884 Mon Sep 17 00:00:00 2001 From: robinvandermolen Date: Tue, 10 Dec 2024 16:37:21 +0100 Subject: [PATCH] :sparkles: [open-formulieren/open-forms#2177] Get coordinates of geo json --- src/components/Map/index.jsx | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/components/Map/index.jsx b/src/components/Map/index.jsx index 841083f31..9ace49cc4 100644 --- a/src/components/Map/index.jsx +++ b/src/components/Map/index.jsx @@ -69,6 +69,15 @@ const useDefaultCoordinates = () => { return [latitude, longitude]; }; +const getCoordinates = geoJsonFeature => { + if (!geoJsonFeature) { + return null; + } + + const center = Leaflet.geoJSON(geoJsonFeature).getBounds().getCenter(); + return [center.lat, center.lng]; +}; + const LeaftletMap = ({ geoJsonFeature, onGeoJsonFeatureSet, @@ -81,7 +90,8 @@ const LeaftletMap = ({ const featureGroupRef = useRef(); const intl = useIntl(); const defaultCoordinates = useDefaultCoordinates(); - const coordinates = defaultCoordinates; + const geoJsonCoordinates = getCoordinates(geoJsonFeature); + const coordinates = geoJsonCoordinates ?? defaultCoordinates; const modifiers = disabled ? ['disabled'] : []; const className = getBEMClassName('leaflet-map', modifiers); @@ -142,11 +152,7 @@ const LeaftletMap = ({ }} /> - {coordinates ? ( - <> - - - ) : null} + {coordinates && } {/*{disabled ? : }*/} - {/*{markerCoordinates && markerCoordinates.length && (*/} - {/* */} - {/*)}*/} + {geoJsonCoordinates && geoJsonCoordinates.length && ( + + )} ); };