Raster Tiles
Runnable MapLibre GL JS example: Raster 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
<!--
示例:栅格瓦片
核心逻辑:raster 源加载 256 像素瓦片,滑块调透明度,切换明暗两套瓦片地址
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式 · CARTO 免 Key 栅格瓦片
-->
<!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:110px}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<label>透明度 <input id="op" type="range" min="0" max="1" step="0.05" value="1"> <b id="opv">1.00</b></label>
<label><input id="dark" type="checkbox"> 深色瓦片</label>
<span>tileSize <b>256</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 LIGHT = ['https://basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png'];
var DARK = ['https://basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png'];
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: 'Tiles © Esri © CARTO'
}
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
map.on('load', function () {
map.addSource('raster-base', {
type: 'raster',
tiles: LIGHT,
tileSize: 256,
minzoom: 0,
maxzoom: 19,
attribution: 'Tiles © Esri © CARTO'
});
map.addLayer({
id: 'raster-base',
type: 'raster',
source: 'raster-base',
paint: {
'raster-opacity': 1,
'raster-fade-duration': 120
}
});
});
document.getElementById('op').addEventListener('input', function () {
var v = parseFloat(this.value);
document.getElementById('opv').textContent = v.toFixed(2);
if (map.getLayer('raster-base')) {
map.setPaintProperty('raster-base', 'raster-opacity', v);
}
});
document.getElementById('dark').addEventListener('change', function () {
var src = map.getSource('raster-base');
if (!src) return;
src.setTiles(this.checked ? DARK : LIGHT);
});
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.