百度地图api实现轨迹运动效果
作者:互联网
百度地图api实现轨迹运动效果
实现逻辑:
实现轨迹运动的效果无非就是将一段路程细化为很多个点,然后不过的根据这些点来画出移动轨迹。
实现流程:
1)使用DrivingRoute,根据起点和终点的位置,自动生成路径点。
2)建立一个定时任务,使用Polyline不断的通过这些点渲染出移动轨迹。
3)使用Marker的setPosition改变标注点位置,实现播放功能。
直接上代码,完全可用:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<style type="text/css">
html {
height: 100%
}
body {
height: 100%;
margin: 0px;
padding: 0px
}
#controller {
width: 100%;
border-bottom: 3px outset;
height: 30px;
filter: alpha(Opacity=100);
-moz-opacity: 1;
opacity: 1;
z-index: 10000;
background-color: lightblue;
}
#container {
height: 100%
}
</style>
<script src="http://mapopen.cdn.bcebos.com/github/BMapGLLib/TrackAnimation/src/TrackAnimation.min.js"></script>
<script type="text/javascript"
src="http://api.map.baidu.com/api?v=2.0&ak=你的密钥"></script>
</head>
<body>
<div id="controller">
<input id="follow" type="checkbox"><span style="font-size:12px;">画面跟随</span></input>
<input id="play" type="button" value="播放" disabled />
<input id="pause" type="button" value="暂停" disabled />
<input id="reset" type="button" value="重置" disabled />
<span>速度:</span> <input type="number" id="speed" value="50" step="50" min="20">
</div>
<div id="container"></div>
</body>
<script type="text/javascript">
var map = new BMap.Map("container");
//获取所有点的坐标
var points = [
new BMap.Point(114.00100, 22.550000), new BMap.Point(114.00130, 22.550000),
new BMap.Point(114.00160, 22.550000), new BMap.Point(114.00200, 22.550000),
new BMap.Point(114.00300, 22.550500), new BMap.Point(114.00400, 22.550000),
new BMap.Point(114.00500, 22.550000), new BMap.Point(114.00505, 22.549800),
new BMap.Point(114.00510, 22.550000), new BMap.Point(114.00515, 22.550000),
new BMap.Point(114.00525, 22.550400), new BMap.Point(114.00537, 22.549500)
];
var rangePoint = [
new BMap.Point(114.00100, 22.550000),
new BMap.Point(113.955100, 22.550020),
new BMap.Point(113.855100, 22.550030),
new BMap.Point(113.755100, 22.550040),
new BMap.Point(113.655100, 22.550050)
]
var map; //百度地图对象
var car; //汽车图标
var label; //信息标签
var centerPoint;
var totalTime = 0; //根据点的数组长度和速度,计算路程的长度
var pastTime = 0; //点已经走的时间
var rangeLen = 0;
var judge = true;
var timer; //定时器
var index = 0; //记录播放到第几个point
var followChk, playBtn, pauseBtn, resetBtn; //几个控制按钮
followChk = true;
playBtn = document.getElementById("play");
pauseBtn = document.getElementById("pause");
resetBtn = document.getElementById("reset");
//初始化地图,选取第一个点为起始点
map.centerAndZoom(points[0], 17);
map.enableScrollWheelZoom();
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.ScaleControl());
map.addControl(new BMap.OverviewMapControl({ isOpen: true }));
//通过DrivingRoute获取一条路线的point
var driving = new BMap.DrivingRoute(map); //可以使用WalkingRoute或RidingRoute,api生成的路径时不一样的
driving.search(rangePoint[rangeLen], rangePoint[rangeLen + 1]); //起点和终点
driving.setSearchCompleteCallback(function () {
//得到路线上的所有point
points = driving.getResults().getPlan(0).getRoute(0).getPath(); //根据起点和终点,拿到路径上所有的点
totalTime = points.length * 50; // 总时间
map.panTo(new BMap.Point(114.00100, 22.550000)); //视角移到起点
//显示小车子
label = new BMap.Label("", { offset: new BMap.Size(-20, -20) });
car = new BMap.Marker(points[0]);
car.setLabel(label);
map.addOverlay(car);
//点亮操作按钮
playBtn.disabled = false;
resetBtn.disabled = false;
rangeLen++
});
var btn1 = document.getElementById('play');
btn1.addEventListener('click', function () { play(50) }, false);
function play(speed) {
playBtn.disabled = true;
pauseBtn.disabled = false;
pastTime += speed;
var point = points[index];
if (index > 0) {
map.addOverlay(new BMap.Polyline([points[index - 1], point], { strokeColor: "red", strokeWeight: 1, strokeOpacity: 1 }));
}
label.setContent("经度: " + point.lng + "<br>纬度: " + point.lat);
car.setPosition(point);
index++;
if (totalTime - pastTime <= 1500 && judge && rangeLen < rangePoint.length) {
judge = false;
addPoints()
}
if (followPoint(point)) {
map.centerAndZoom(point, 17);
}
if (index < points.length) {
timer = window.setTimeout("play(" + speed + ")", speed);
} else {
playBtn.disabled = true;
pauseBtn.disabled = true;
map.panTo(point);
}
}
var btn2 = document.getElementById('pause');
btn2.addEventListener('click', pause, false);
function pause() {
playBtn.disabled = false;
pauseBtn.disabled = true;
if (timer) {
window.clearTimeout(timer);
}
}
var btn3 = document.getElementById('reset');
btn3.addEventListener('click', reset, false);
function reset() {
followChk.checked = false;
playBtn.disabled = false;
pauseBtn.disabled = true;
if (timer) {
window.clearTimeout(timer);
}
index = 0;
car.setPosition(points[0]);
map.panTo(centerPoint);
}
var btn4 = document.getElementById('speed');
btn4.addEventListener('change', speed, false);
function speed(e) {
var speed1 = parseInt(this.value)
window.clearTimeout(timer)
play(speed1)
}
// 当点移动出范围以后,跟随点
function followPoint(p) {
var bound = map.getBounds();//地图可视区域
if (bound.containsPoint(p) != true) {
console.log("超出")
return true
} else {
return false
}
}
function addPoints() {
driving.search(rangePoint[rangeLen], rangePoint[rangeLen + 1]); //起点和终点
driving.setSearchCompleteCallback(function () {
var pwe = driving.getResults().getPlan(0).getRoute(0).getPath(); //根据起点和终点,拿到路径上所有的点
points = points.concat(pwe)
totalTime = points.length * 50;
rangeLen++
judge = true
})
}
</script>
</html>
参考:https://blog.csdn.net/xy2204/article/details/46049413
标签:map,轨迹,false,Point,api,BMap,var,new,百度 来源: https://blog.csdn.net/weixin_45663625/article/details/120570517