Flight Animation
Runnable OpenLayers example: Flight Animation. 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
<!--
说明:飞行动画
核心逻辑:按进度取航线坐标推进飞机,追加尾迹并用 view.setCenter 跟随
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!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/ol@10.3.1/ol.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}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:10px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar button{font:inherit;padding:4px 10px;border:1px solid #c9d0da;border-radius:5px;
background:#fff;color:#1f2a37;cursor:pointer}
#info{margin-left:auto}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<button id="play">起飞</button>
<button id="reset">重置</button>
<span id="info">—</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('map').innerHTML =
'<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var fromLonLat = ol.proj.fromLonLat;
var routeLonLat = [
[116.391, 39.907], [113.30, 40.10], [110.00, 38.50],
[107.00, 36.00], [103.80, 36.06], [101.80, 36.62]
];
var route = new ol.geom.LineString(routeLonLat.map(fromLonLat));
var trail = new ol.geom.LineString([route.getCoordinateAt(0)]);
var trailFeature = new ol.Feature({ geometry: trail });
var plane = new ol.Feature({ geometry: new ol.geom.Point(route.getCoordinateAt(0)) });
plane.set('rot', 0);
var source = new ol.source.Vector({ features: [trailFeature, plane] });
var layer = new ol.layer.Vector({
source: source,
style: function (feature) {
if (feature === trailFeature) {
return new ol.style.Style({
stroke: new ol.style.Stroke({ color: 'rgba(226,110,40,0.65)', width: 3 })
});
}
return new ol.style.Style({
image: new ol.style.RegularShape({
points: 3,
radius: 10,
rotation: feature.get('rot'),
fill: new ol.style.Fill({ color: '#c8281c' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 2 })
})
});
}
});
var view = new ol.View({ center: fromLonLat([116.391, 39.907]), zoom: 6, minZoom: 3, maxZoom: 19 });
var map = new ol.Map({
target: 'map',
layers: [new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }), layer],
view: view,
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
var info = document.getElementById('info');
var progress = 0;
var running = false;
var timer = null;
var distanceKm = ol.sphere.getLength(route) / 1000;
function draw() {
var coord = route.getCoordinateAt(progress);
var ahead = route.getCoordinateAt(Math.min(progress + 0.002, 1));
var dx = ahead[0] - coord[0];
var dy = ahead[1] - coord[1];
var rot = Math.atan2(dy, dx) + Math.PI / 2;
plane.getGeometry().setCoordinates(coord);
plane.set('rot', rot);
var coords = trail.getCoordinates();
if (coords.length === 0 || ol.coordinate.squaredDistance(coords[coords.length - 1], coord) > 1) {
coords.push(coord);
trail.setCoordinates(coords);
}
view.setCenter(coord);
info.textContent = '进度 ' + Math.round(progress * 100) + '% · 全程约 ' +
Math.round(distanceKm) + ' km';
}
function tick() {
if (!running) return;
progress += 0.0018;
if (progress >= 1) {
progress = 1;
running = false;
document.getElementById('play').textContent = '起飞';
}
draw();
}
document.getElementById('play').addEventListener('click', function () {
running = !running;
this.textContent = running ? '暂停' : '起飞';
if (running && progress >= 1) progress = 0;
if (running) {
clearInterval(timer);
timer = setInterval(tick, 30);
} else {
clearInterval(timer);
}
});
document.getElementById('reset').addEventListener('click', function () {
clearInterval(timer);
running = false;
progress = 0;
document.getElementById('play').textContent = '起飞';
trail.setCoordinates([route.getCoordinateAt(0)]);
draw();
view.setCenter(route.getCoordinateAt(0));
view.setZoom(6);
});
draw();
})();
</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.