Maplibre GL - Ebenen ein- und ausblenden
Erstelle einen benutzerdefinierten Ebenenumschalter, um verschiedene Datensätze anzuzeigen.
Das Beispiel im Bild zeigt dir, wie du ein Dreieck und ein Polygon ein- und ausblendest. Dieses kommt aus dem unten verlinkten Gatsby-Starter und ist bewusst unkompliziert gehalten. Der nachfolgende Programmcode zeigt dir ein realeres Szenario, welches ebenfalls als Demo unten verlinkt ist.
<!-- https://raw.githubusercontent.com/astridx/maplibreexamples/main/examples/show-and-hide-layers.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Show and hide layers</title>
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<script src="../.env"></script>
<script src="https://unpkg.com/maplibre-gl@1.14.0-rc.1/dist/maplibre-gl.js"></script>
<link
href="https://unpkg.com/maplibre-gl@1.14.0-rc.1/dist/maplibre-gl.css"
rel="stylesheet"
/>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<style>
#menu {
background: #fff;
position: absolute;
z-index: 1;
top: 10px;
right: 10px;
border-radius: 3px;
width: 120px;
border: 1px solid rgba(0, 0, 0, 0.4);
font-family: 'Open Sans', sans-serif;
}
#menu a {
font-size: 13px;
color: #404040;
display: block;
margin: 0;
padding: 0;
padding: 10px;
text-decoration: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.25);
text-align: center;
}
#menu a:last-child {
border: none;
}
#menu a:hover {
background-color: #f8f8f8;
color: #404040;
}
#menu a.active {
background-color: #3887be;
color: #ffffff;
}
#menu a.active:hover {
background: #3074a4;
}
</style>
<nav id="menu"></nav>
<div id="map"></div>
<script>
var map = new maplibregl.Map({
container: 'map',
style:
'https://api.maptiler.com/maps/streets/style.json?key=' +
config.MAPTILER_TOKEN,
zoom: 4,
center: [7.13734351262877, 50.137451890638886],
})
// https://github.com/astridx/world.geo.json/tree/master/countries
map.on('load', function () {
map.addSource('deutschland_fill', {
type: 'geojson',
data: 'https://raw.githubusercontent.com/astridx/world.geo.json/master/countries/DEU.geo.json',
})
map.addLayer({
id: 'deutschland_fill',
type: 'fill',
source: 'deutschland_fill',
layout: {
visibility: 'none',
},
paint: {
'fill-color': '#0000ff',
'fill-opacity': 0.4,
},
})
map.addSource('deutschland_line', {
type: 'geojson',
data: 'https://raw.githubusercontent.com/astridx/world.geo.json/master/countries/DEU.geo.json',
})
map.addLayer({
id: 'deutschland_line',
type: 'line',
source: 'deutschland_line',
layout: {
visibility: 'none',
},
})
})
var toggleableLayerIds = ['deutschland_line', 'deutschland_fill']
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i]
var link = document.createElement('a')
link.href = '#'
link.className = 'active'
link.textContent = id
link.onclick = function (e) {
var clickedLayer = this.textContent
e.preventDefault()
e.stopPropagation()
var visibility = map.getLayoutProperty(clickedLayer, 'visibility')
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none')
this.className = ''
} else {
this.className = 'active'
map.setLayoutProperty(clickedLayer, 'visibility', 'visible')
}
}
var layers = document.getElementById('menu')
layers.appendChild(link)
}
</script>
</body>
</html>
Demo
Quellcode
Gatsby Starter mit dieser Funktion - Gatsby Starter Demo
MapBox GL Beispiel