6. Layertree
The LayerList and LayerListItem components will help us to build a custom layer tree.
Add Layers Component to the Sidebar
-
The existing
Layerscomponent 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
LayerListandLayerListItemneed to be imported:import {
LayerList,
LayerListItem,
MlGeoJsonLayer,
MlVectorTileLayer,
} from "@mapcomponents/react-maplibre"; -
... and integrated into the JSX:
We need a
LayerListcomponent and for each existinglayercomponent aLayerListItemcomponent. Thelayercomponent is passed in thelayerComponentattribute.For the
LayerListItemcomponent of theMlVectorTileLayer, we pass the setter function of thebgLayerStatestate variable in thesetLayerStateattribute.For the
LayerListItemcomponent of theMlGeoJsonLayer, we pass theMlGeoJsonLayerComponent. Because the GeoJSON file contains polygons, we can integrate it as both afilland alinelayer.The LayerListItem components are conditionally included based on the respective state variables
laenderorbgLayerState.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>