Skip to content

Commit

Permalink
✨ [open-formulieren/open-forms#2177] Get coordinates of geo json
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmolen committed Dec 10, 2024
1 parent 172ffd0 commit 891d55f
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/components/Map/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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,
Expand All @@ -74,7 +83,8 @@ const LeaftletMap = ({
const ref = 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);
Expand Down Expand Up @@ -141,11 +151,7 @@ const LeaftletMap = ({
}}
/>
</FeatureGroup>
{coordinates ? (
<>
<MapView coordinates={coordinates} />
</>
) : null}
{coordinates && <MapView coordinates={coordinates} />}
<SearchControl
onMarkerSet={onSearchMarkerSet}
options={{
Expand All @@ -162,9 +168,9 @@ const LeaftletMap = ({
/>
{/*{disabled ? <DisabledMapControls /> : <CaptureClick setMarker={onMarkerSet} />}*/}
</MapContainer>
{/*{markerCoordinates && markerCoordinates.length && (*/}
{/* <NearestAddress coordinates={markerCoordinates} />*/}
{/*)}*/}
{geoJsonCoordinates && geoJsonCoordinates.length && (
<NearestAddress coordinates={geoJsonCoordinates} />
)}
</>
);
};
Expand Down

0 comments on commit 891d55f

Please sign in to comment.