代码
import {CatmullRomCurve3, Group, Vector3} from 'three';
import {
BaiduMapConfig,
BaiduVectorTileProvider,
DynamicSky,
Engine,
GeoJSONDataSource,
PathTracker,
Polyline,
gltfLoader,
twinConstants,
} from '@baidumap/mapv-three';
BaiduMapConfig.ak = '您的密钥';
const center = [116.4028, 39.9343];
const container = document.getElementById('map');
const aspect = container.clientWidth / container.clientHeight;
const engine = new Engine(container, {
map: {
projection: 'EPSG:3857',
center,
range: 685 / Math.min(1, aspect * 1.4),
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.40579, 39.93425],
[116.40578, 39.93451],
[116.40576, 39.93462],
[116.40568, 39.93472],
[116.40554, 39.93478],
[116.40532, 39.9348],
[116.40455, 39.93479],
[116.40292, 39.93477],
[116.40068, 39.93474],
].map(([lng, lat]) => new Vector3().fromArray(engine.map.projectArrayCoordinate([lng, lat, 0.4])));
const curve = new CatmullRomCurve3(waypoints);
const path = curve
.getSpacedPoints(80)
.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());
target.position.copy(waypoints[0]);
const direction = curve.getTangentAt(0);
target.rotation.z = Math.atan2(direction.y, direction.x);
async function loadCar() {
const modelUrl = twinConstants.REALISTIC_TEMPLATE_MODEL.CAR;
const {scene} = await gltfLoader.loadAsync(modelUrl);
// 轿车模型的 X 轴指向车头,将 Y 轴向上转换为地图的 Z 轴向上。
scene.rotation.x = Math.PI / 2;
scene.scale.setScalar(2.4);
target.add(scene);
engine.requestRender();
}
const tracker = engine.add(new PathTracker());
tracker.track = path;
tracker.object = target;
const viewMode = document.querySelector('#view-mode');
const play = document.querySelector('#play');
const pause = document.querySelector('#pause');
const stop = document.querySelector('#stop');
tracker.viewMode = viewMode.value;
viewMode.onchange = () => {
tracker.viewMode = viewMode.value;
};
function updateControls() {
const running = tracker.isRunning;
play.disabled = running && !tracker.isPaused;
play.textContent = tracker.isPaused ? '继续' : '播放';
pause.disabled = !running || tracker.isPaused;
stop.disabled = !running;
play.classList.toggle('active', !play.disabled);
pause.classList.toggle('active', !pause.disabled);
}
play.onclick = () => {
tracker.start({
duration: 16_000,
range: 195,
pitch: 52,
});
updateControls();
};
pause.onclick = () => {
tracker.pause();
updateControls();
};
stop.onclick = () => tracker.stop();
tracker.onFinish = updateControls;
loadCar().catch(() => {
play.title = '轿车模型加载失败,仍可沿路线漫游';
});
让 SDK 内置轿车沿景山公园旁的道路漫游。默认锁定视角随车转弯,可切换为保留地图朝向的跟随视角或自行操作地图的自由视角。
预览