Fly to
Runnable MapLibre GL JS example: Fly to. 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
<!--
示例:飞行动画
核心逻辑:按钮触发 flyTo,带俯仰与旋转,动画结束后回读视角
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 免 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:8px;align-items:center;padding:8px 12px;
background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:4px;cursor:pointer;
border:1px solid #ccd1d9;background:#fff;color:#16181d}
.bar button:hover{background:#eef0f3}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<button data-city="beijing">北京</button>
<button data-city="shanghai">上海</button>
<button data-city="newyork">纽约</button>
<button data-city="capetown">开普敦</button>
<button data-city="sydney">悉尼</button>
<span>当前 <b id="cur">—</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 places = {
beijing: { name: '北京', center: [116.391, 39.907], zoom: 4, pitch: 40, bearing: -18 },
shanghai: { name: '上海', center: [121.474, 31.230], zoom: 4, pitch: 45, bearing: 24 },
newyork: { name: '纽约', center: [-74.006, 40.713], zoom: 3.6, pitch: 35, bearing: -32 },
capetown: { name: '开普敦', center: [18.424, -33.925], zoom: 3.8, pitch: 42, bearing: 12 },
sydney: { name: '悉尼', center: [151.209, -33.868], zoom: 3.4, pitch: 30, bearing: 40 }
};
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [104, 34],
zoom: 2.6,
maxPitch: 70,
attributionControl: {
compact: true,
customAttribution: 'MapLibre demo tiles'
}
});
function fly(key) {
var p = places[key];
map.flyTo({
center: p.center,
zoom: p.zoom,
pitch: p.pitch,
bearing: p.bearing,
speed: 0.9,
curve: 1.4,
duration: 2600,
essential: true
});
document.getElementById('cur').textContent = p.name + '(飞行中)';
}
document.querySelectorAll('[data-city]').forEach(function (btn) {
btn.addEventListener('click', function () {
fly(this.getAttribute('data-city'));
});
});
map.on('moveend', function () {
var c = map.getCenter();
document.getElementById('cur').textContent =
c.lng.toFixed(2) + ', ' + c.lat.toFixed(2) +
' · z' + map.getZoom().toFixed(1) +
' · ' + map.getPitch().toFixed(0) + '°';
});
map.addControl(new maplibregl.NavigationControl({ visualizePitch: true }), 'top-right');
})();
</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.