6. Layertree
The LayerList
and LayerListItem
components will help us to build a custom layer tree.
Add Layers
Component to the Sidebar
-
The existing
Layers
component needs to be a child of the Sidebar:<Sidebar open={true} name={"Sidebar"}>
<Layers />
</Sidebar>
Add LayerList
and LayerListItem
Component to the Sidebar
-
The components
LayerList
andLayerListItem
need to be imported:import {
LayerList,
LayerListItem,
MlGeoJsonLayer,
MlVectorTileLayer,
} from "@mapcomponents/react-maplibre"; -
... and integrated into the JSX:
We need a
LayerList
component and for each existinglayer
component aLayerListItem
component. Thelayer
component is passed in thelayerComponent
attribute.For the
LayerListItem
component of theMlVectorTileLayer
, we pass the setter function of thebgLayerState
state variable in thesetLayerState
attribute.For the
LayerListItem
component of theMlGeoJsonLayer
, we pass theMlGeoJsonLayer
Component. Because the GeoJSON file contains polygons, we can integrate it as both afill
and aline
layer.The LayerListItem components are conditionally included based on the respective state variables
laender
orbgLayerState.layers
.<LayerList>
{bgLayerState.layers && (
<LayerListItem
name="Background"
setLayerState={setBgLayerState}
configurable={false}
layerComponent={<MlVectorTileLayer {...bgLayerState} />}
/>
)}
{laender && (
<LayerListItem
name="Länder"
layerComponent={
<MlGeoJsonLayer
insertBeforeLayer="waterway-name"
mapId="map_1"
geojson={laender}
/>
}
/>
)}
{laender && (
<LayerListItem
name="Länder Outline"
configurable={true}
layerComponent={
<MlGeoJsonLayer
type="line"
insertBeforeLayer="waterway-name"
mapId="map_1"
geojson={laender}
/>
}
/>
)}
</LayerList>