Layer Visibility
Runnable MapLibre GL JS example: Layer Visibility. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:图层显隐
核心逻辑:setLayoutProperty 切换 visibility,分别控制面、线、标注三个图层
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>图层显隐</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
.bar{flex:0 0 auto;display:flex;gap:14px;align-items:center;padding:8px 12px;
background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
.btn{border:1px solid #cdd3db;background:#fff;border-radius:4px;padding:3px 9px;
font:12px/1.5 inherit;color:#16181d;cursor:pointer}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<label><input type="checkbox" data-layer="zone-fill" checked> 面</label>
<label><input type="checkbox" data-layer="zone-line" checked> 线</label>
<label><input type="checkbox" data-layer="zone-label" checked> 标注</label>
<button class="btn" id="all">全部切换</button>
<span>状态 <b id="state">三层可见</b></span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
'use strict';
if (typeof maplibregl === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var zones = {
type: 'FeatureCollection',
features: [
{ type: 'Feature', properties: { name: 'NORTH' },
geometry: { type: 'Polygon', coordinates: [[[98, 38], [108, 39], [107, 43], [97, 42], [98, 38]]] } },
{ type: 'Feature', properties: { name: 'EAST' },
geometry: { type: 'Polygon', coordinates: [[[112, 31], [121, 31], [122, 37], [112, 37], [112, 31]]] } },
{ type: 'Feature', properties: { name: 'SOUTH' },
geometry: { type: 'Polygon', coordinates: [[[104, 23], [114, 23], [115, 28], [104, 29], [104, 23]]] } },
{ type: 'Feature', properties: { name: 'NORTH' },
geometry: { type: 'Point', coordinates: [102.5, 40.5] } },
{ type: 'Feature', properties: { name: 'EAST' },
geometry: { type: 'Point', coordinates: [117.0, 34.0] } },
{ type: 'Feature', properties: { name: 'SOUTH' },
geometry: { type: 'Point', coordinates: [109.5, 25.5] } }
]
};
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [109.5, 33.0],
zoom: 3.4,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
map.on('load', function () {
map.addSource('zones', { type: 'geojson', data: zones });
map.addLayer({
id: 'zone-fill',
type: 'fill',
source: 'zones',
filter: ['==', ['geometry-type'], 'Polygon'],
paint: { 'fill-color': '#8ab4e8', 'fill-opacity': 0.35 }
});
map.addLayer({
id: 'zone-line',
type: 'line',
source: 'zones',
filter: ['==', ['geometry-type'], 'Polygon'],
paint: { 'line-color': '#2f6fbd', 'line-width': 2 }
});
map.addLayer({
id: 'zone-label',
type: 'symbol',
source: 'zones',
filter: ['==', ['geometry-type'], 'Point'],
layout: {
'text-field': ['get', 'name'],
'text-font': ['Noto Sans Regular'],
'text-size': 14
},
paint: {
'text-color': '#15304a',
'text-halo-color': '#ffffff',
'text-halo-width': 1.5
}
});
});
function setVisible(layerId, visible) {
if (!map.getLayer(layerId)) return;
map.setLayoutProperty(layerId, 'visibility', visible ? 'visible' : 'none');
}
function report() {
var on = [];
document.querySelectorAll('[data-layer]').forEach(function (box) {
if (box.checked) on.push(box.parentNode.textContent.trim());
});
document.getElementById('state').textContent =
on.length === 0 ? '全部隐藏' : on.join('、') + ' 可见';
}
document.querySelectorAll('[data-layer]').forEach(function (box) {
box.addEventListener('change', function () {
setVisible(this.getAttribute('data-layer'), this.checked);
report();
});
});
document.getElementById('all').addEventListener('click', function () {
var boxes = document.querySelectorAll('[data-layer]');
var anyOn = Array.prototype.some.call(boxes, function (b) { return b.checked; });
Array.prototype.forEach.call(boxes, function (b) {
b.checked = !anyOn;
setVisible(b.getAttribute('data-layer'), !anyOn);
});
report();
});
map.on('error', function (e) {
if (e.error) console.warn('地图错误:' + e.error.message);
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.