5. Data Integration
Display the federal states using the MlGeoJsonLayer component on the map.
The dataset can be accessed at the following URL: https://raw.githubusercontent.com/isellsoap/deutschlandGeoJSON/main/2_bundeslaender/4_niedrig.geo.json
The file components/Layers.jsx will now be extended:
-
Add MlGeoJMlGeoJsonLayer-Component:
import {MlGeoJsonLayer} from "@mapcomponents/react-maplibre"
insrc/components/Layers.jsx
. -
Define the
State Variable
that will hold the GeoJSON data:const [laender, setLaender] = useState();
-
Fetch the GeoJSON Data: Use an effect to fetch the data when the component mounts. Once the data is fetched, store it in a
state variable
. Now, integrate this operation into the existinguseEffect
hook that is used for fetching background tiles.useEffect(() => {
fetch("https://wms.wheregroup.com/tileserver/style/osm-bright.json")
.then((response) => response.json())
.then((data) =>
setBgLayerState((current) => {
return { ...current, layers: data.layers };
})
);
fetch(
"https://raw.githubusercontent.com/isellsoap/deutschlandGeoJSON/main/2_bundeslaender/4_niedrig.geo.json"
)
.then((res) => res.json())
.then((data) => setLaender(data));
}, []); -
Include the MlGeoJMlGeoJsonLayer Component: In the JSX, add the MlGeoJMlGeoJsonLayer component.
{
laender && (
<MlGeoJsonLayer
insertBeforeLayer="waterway-name"
mapId="map_1"
geojson={laender}
/>
);
}