代码
import {CatmullRomCurve3, Group, Vector3} from 'three';
import {
BaiduMapConfig,
BaiduVectorTileProvider,
DynamicSky,
Engine,
GeoJSONDataSource,
ObjectTracker,
Polyline,
gltfLoader,
twinConstants,
} from '@baidumap/mapv-three';
BaiduMapConfig.ak = '您的密钥';
const center = [116.40285, 39.93206];
const container = document.getElementById('map');
const aspect = container.clientWidth / container.clientHeight;
const engine = new Engine(container, {
map: {
projection: 'EPSG:3857',
center,
range: 900 / Math.min(1, aspect * 1.2),
pitch: 42,
provider: new BaiduVectorTileProvider({
displayOptions: {
flat: true,
building: false,
},
}),
},
rendering: {
sky: null,
enableAnimationLoop: true,
},
clock: {
currentTime: new Date(2026, 5, 21, 16),
},
});
engine.add(new DynamicSky());
// 景山公园旁的 BD-09 道路坐标,沿曲线等距采样,使车辆平滑转弯。
const waypoints = [
[116.40599, 39.92988],
[116.40592, 39.9317],
[116.40582, 39.93373],
[116.40578, 39.93451],
[116.40576, 39.93462],
[116.40568, 39.93472],
[116.40554, 39.93478],
[116.40532, 39.9348],
[116.40462, 39.93479],
[116.40291, 39.93477],
[116.40042, 39.93474],
[116.40018, 39.9347],
[116.40001, 39.9346],
[116.39994, 39.93445],
[116.39997, 39.93336],
[116.40006, 39.93113],
[116.40013, 39.92975],
[116.40019, 39.92962],
[116.40036, 39.92953],
[116.40101, 39.92955],
[116.40313, 39.92963],
[116.40549, 39.92972],
[116.40586, 39.92974],
].map(([lng, lat]) => new Vector3().fromArray(engine.map.projectArrayCoordinate([lng, lat, 0.4])));
const curve = new CatmullRomCurve3(waypoints, true);
const path = curve
.getSpacedPoints(160)
.map((point) => engine.map.unprojectArrayCoordinate(point.toArray()));
const line = engine.add(
new Polyline({
color: '#216bc1',
keepSize: true,
lineWidth: 4,
}),
);
line.dataSource = GeoJSONDataSource.fromGeoJSON([
{
type: 'Feature',
geometry: {type: 'LineString', coordinates: path},
},
]);
const target = engine.add(new Group());
const direction = new Vector3();
function moveCar(progress) {
curve.getPointAt(progress, target.position);
curve.getTangentAt(progress, direction);
target.rotation.z = Math.atan2(direction.y, direction.x);
}
moveCar(0);
let disposed = false;
async function loadCar() {
const modelUrl = twinConstants.REALISTIC_TEMPLATE_MODEL.CAR;
const {scene} = await gltfLoader.loadAsync(modelUrl);
if (disposed) return;
// 轿车模型的 X 轴指向车头,将 Y 轴向上转换为地图的 Z 轴向上。
scene.rotation.x = Math.PI / 2;
scene.scale.setScalar(2.4);
target.add(scene);
engine.requestRender();
}
const tracker = engine.add(new ObjectTracker());
const duration = 105_000;
let startedAt;
let frame;
function move(time) {
startedAt ??= time;
moveCar(((time - startedAt) % duration) / duration);
engine.requestRender();
frame = requestAnimationFrame(move);
}
frame = requestAnimationFrame(move);
const start = document.querySelector('#follow');
const stop = document.querySelector('#stop');
function updateControls(following) {
start.disabled = following;
stop.disabled = !following;
start.classList.toggle('active', !following);
stop.classList.toggle('active', following);
}
loadCar()
.then(() => {
if (disposed) return;
start.textContent = '跟随车辆';
updateControls(false);
})
.catch(() => {
if (!disposed) start.textContent = '车辆模型加载失败';
});
start.onclick = () => {
tracker.track(target, {range: 170, lock: false});
updateControls(true);
};
stop.onclick = () => {
tracker.stop();
updateControls(false);
};
window.addEventListener('pagehide', ({persisted}) => {
if (persisted) return;
disposed = true;
cancelAnimationFrame(frame);
tracker.stop();
engine.dispose();
});
SDK 内置轿车沿景山公园周边道路行驶。开始跟随后,镜头随车移动并保留地图朝向。停止跟随后,车辆继续行驶。
预览