代码
const map = new BMap.Map('map');
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
map.enableScrollWheelZoom();
// 示例路径(北京附近折线)
var path = [
new BMap.Point(116.404, 39.915),
new BMap.Point(116.414, 39.92),
new BMap.Point(116.424, 39.912),
new BMap.Point(116.436, 39.905),
new BMap.Point(116.45, 39.91),
];
var polyline = new BMap.Polyline(path, {
strokeColor: '#1a73e8',
strokeWeight: 4,
strokeOpacity: 0.9,
});
map.addOverlay(polyline);
map.setViewport(path);
var ta = new BMapLib.TrackAnimation(map, polyline, {
duration: 8000, // 动画总时长(ms):轨迹从起点“画到终点”耗时
delay: 0, // 延迟开始(ms)
followView: true, // 播放中是否跟随当前轨迹点移动视口
followViewSmoothing: 1, // 跟随平滑系数(0~1)
followViewEdgePadding: 0.3, // 触发跟随的留白比例(0~1)
overallView: true, // 播放结束后是否 setViewport 展示整条轨迹(播放中不做总览)
onAnimateEnd: function () {
console.log('end');
},
});
function setSpeed(x) {
ta.setSpeed(x);
}
预览