Vector Tiles
Runnable MapLibre GL JS example: Vector Tiles. 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
<!--
示例:矢量瓦片
核心逻辑:注册 vector 源指向 PBF 瓦片,按 source-layer 分别添加面、线、标注图层
依赖: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}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<span>源 <b>vector · maxzoom 6</b></span>
<span>源图层 <b>countries / centroids</b></span>
<span>缩放 <b id="z">—</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 map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [104.2, 35.5],
zoom: 3,
attributionControl: {
compact: true,
customAttribution: 'MapLibre demo tiles'
}
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
map.on('load', function () {
map.addSource('world', {
type: 'vector',
tiles: ['https://demotiles.maplibre.org/tiles/{z}/{x}/{y}.pbf'],
minzoom: 0,
maxzoom: 6,
attribution: 'MapLibre demo tiles'
});
map.addLayer({
id: 'world-tint',
type: 'fill',
source: 'world',
'source-layer': 'countries',
paint: {
'fill-color': [
'match', ['get', 'CONTINENT'],
'Asia', '#cfe3f5',
'Europe', '#e0daf2',
'Africa', '#f2e7cf',
'North America', '#d9eee0',
'South America', '#f5dede',
'Oceania', '#e3ead0',
'#e6eaf0'
],
'fill-opacity': 0.7
}
});
map.addLayer({
id: 'world-edge',
type: 'line',
source: 'world',
'source-layer': 'countries',
paint: {
'line-color': '#5b7c9d',
'line-width': ['interpolate', ['linear'], ['zoom'], 2, 0.4, 6, 1.2]
}
});
map.addLayer({
id: 'world-label',
type: 'symbol',
source: 'world',
'source-layer': 'centroids',
layout: {
'text-field': ['get', 'NAME'],
'text-font': ['Noto Sans Regular'],
'text-size': ['interpolate', ['linear'], ['zoom'], 2, 9, 6, 13],
'text-transform': 'uppercase'
},
paint: {
'text-color': '#2d3c4d',
'text-halo-color': '#ffffff',
'text-halo-width': 1.2
}
});
});
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.