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 authored and sergei-maertens committed Jan 22, 2025
1 parent 6b20cbb commit ee643c1
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 @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -142,11 +152,7 @@ const LeaftletMap = ({
}}
/>
</FeatureGroup>
{coordinates ? (
<>
<MapView coordinates={coordinates} />
</>
) : null}
{coordinates && <MapView coordinates={coordinates} />}
<SearchControl
onMarkerSet={onSearchMarkerSet}
options={{
Expand All @@ -163,9 +169,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 ee643c1

Please sign in to comment.