Symbol Layer
Runnable MapLibre GL JS example: Symbol Layer. 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
<!--
示例:符号图层
核心逻辑:symbol 图层绑定点位,text-size 按缩放插值,滑块再乘一个字号系数
依赖: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:12px;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 input[type=range]{width:120px}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<label>字号系数 <input id="scale" type="range" min="0.6" max="1.8" step="0.1" value="1"> <b id="scalev">1.0</b></label>
<span>缩放 <b id="z">—</b></span>
<span>标注用 Noto Sans Regular 字形</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 cities = {
type: 'FeatureCollection',
features: [
['Beijing', 116.41, 39.90, 1],
['Shanghai', 121.47, 31.23, 1],
['Guangzhou', 113.26, 23.13, 1],
['Chengdu', 104.07, 30.67, 1],
['Wuhan', 114.31, 30.59, 1],
['Xian', 108.94, 34.34, 1],
['Harbin', 126.53, 45.80, 1],
['Kunming', 102.83, 24.88, 1],
['Lhasa', 91.14, 29.65, 1],
['Urumqi', 87.62, 43.82, 1]
].map(function (c) {
return {
type: 'Feature',
properties: { name: c[0], rank: c[3] },
geometry: { type: 'Point', coordinates: [c[1], c[2]] }
};
})
};
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [108.0, 34.0],
zoom: 3,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
function sizeExpr(factor) {
return ['*', ['interpolate', ['linear'], ['zoom'], 2, 10, 7, 17], factor];
}
map.on('load', function () {
map.addSource('cities', { type: 'geojson', data: cities });
map.addLayer({
id: 'city-dot',
type: 'circle',
source: 'cities',
paint: {
'circle-radius': 4,
'circle-color': '#1f5fd0',
'circle-stroke-color': '#ffffff',
'circle-stroke-width': 1.5
}
});
map.addLayer({
id: 'city-label',
type: 'symbol',
source: 'cities',
layout: {
'text-field': ['get', 'name'],
'text-font': ['Noto Sans Regular'],
'text-size': sizeExpr(1),
'text-offset': [0, 1.2],
'text-anchor': 'top',
'text-allow-overlap': false
},
paint: {
'text-color': '#14263d',
'text-halo-color': '#ffffff',
'text-halo-width': 1.6
}
});
});
document.getElementById('scale').addEventListener('input', function () {
var v = parseFloat(this.value);
document.getElementById('scalev').textContent = v.toFixed(1);
if (map.getLayer('city-label')) {
map.setLayoutProperty('city-label', 'text-size', sizeExpr(v));
}
});
map.on('moveend', function () {
document.getElementById('z').textContent = map.getZoom().toFixed(2);
});
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.