绕点相机动画
使相机绕着一个点旋转动画;
<!DOCTYPE html>
<html lang="en">
<head>
<title>绕点相机动画</title>
<meta property="og:description" content="使相机绕着一个点旋转动画" />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='https://unpkg.com/maplibre-gl@5.5.0/dist/maplibre-gl.css' />
<script src='https://unpkg.com/maplibre-gl@5.5.0/dist/maplibre-gl.js'></script>
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
const map = new maplibregl.Map({
container: 'map',
style:
'https://demotiles.maplibre.org/styles/osm-bright-gl-style/style.json',
center: [-87.62712, 41.89033],
zoom: 15.5,
pitch: 45
});
function rotateCamera(timestamp) {
// 将旋转限制在0-360度之间
// 将timestamp除以100来减慢旋转速度到大约每秒10度
map.rotateTo((timestamp / 100) % 360, {duration: 0});
// 请求动画的下一帧
requestAnimationFrame(rotateCamera);
}
map.on('load', () => {
// 开始动画
rotateCamera(0);
// 添加3D建筑并移除标签图层以增强地图效果
const layers = map.getStyle().layers;
for (let i = 0; i < layers.length; i++) {
if (layers[i].type === 'symbol' && layers[i].layout['text-field']) {
// 移除文本标签
map.removeLayer(layers[i].id);
}
}
map.addLayer({
'id': '3d-buildings',
'source': 'composite',
'source-layer': 'building',
'filter': ['==', 'extrude', 'true'],
'type': 'fill-extrusion',
'minzoom': 15,
'paint': {
'fill-extrusion-color': '#aaa',
// 使用'interpolate'表达式添加平滑过渡效果
// 当用户放大时建筑物会逐渐显示
'fill-extrusion-height': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'height']
],
'fill-extrusion-base': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'min_height']
],
'fill-extrusion-opacity': 0.6
}
});
});
</script>
</body>
</html>