毫无疑问,调用高德地图的API接口,肯定需要加载高德地图的js:
<script src="http://webapi.amap.com/maps?v=1.3&key=你的key值"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
确定主div,用于地图的展示
<div id="container"></div>
设置四个按钮,分别是开始,暂停,继续和停止
<div class="button-group">
<input type="button" class="button" value="开始动画" id="start"/>
<input type="button" class="button" value="暂停动画" id="pause"/>
<input type="button" class="button" value="继续动画" id="resume"/>
<input type="button" class="button" value="停止动画" id="stop"/>
</div>
新建一个地图
cript>
var marker, lineArr = [];
var map = new AMap.Map("container", {
resizeEnable: true,
center: [116.397428, 39.90923],
zoom: 17
});
marker = new AMap.Marker({
map: map,
position: [116.397428, 39.90923],
icon: "http://webapi.amap.com/images/car.png",
offset: new AMap.Pixel(-26, -13),
autoRotation: true
});
插入一些点数据(此处为demo,因此为一些随机生成的实验数据)
var lngX = 116.397428, latY = 39.90923;
lineArr.push([lngX, latY]);
for (var i = 1; i < 4; i++) {
lngX = lngX + Math.random() * 0.05;
if (i % 2) {
latY = latY + Math.random() * 0.0001;
} else {
latY = latY + Math.random() * 0.06;
}
lineArr.push([lngX, latY]);
}
开始绘制历史轨迹(包括历史轨迹的样式)
// 绘制轨迹
var polyline = new AMap.Polyline({
map: map,
path: lineArr,
strokeColor: "#00A", //线颜色
// strokeOpacity: 1, //线透明度
strokeWeight: 3, //线宽
// strokeStyle: "solid" //线样式
});
var passedPolyline = new AMap.Polyline({
map: map,
// path: lineArr,
strokeColor: "#F00", //线颜色
// strokeOpacity: 1, //线透明度
strokeWeight: 3, //线宽
// strokeStyle: "solid" //线样式
});
给四个按钮添加各种事件
marker.on('moving',function(e){
passedPolyline.setPath(e.passedPath);
})
map.setFitView();
AMap.event.addDomListener(document.getElementById('start'), 'click', function() {
marker.moveAlong(lineArr, 500);
}, false);
AMap.event.addDomListener(document.getElementById('pause'), 'click', function() {
marker.pauseMove();
}, false);
AMap.event.addDomListener(document.getElementById('resume'), 'click', function() {
marker.resumeMove();
}, false);
AMap.event.addDomListener(document.getElementById('stop'), 'click', function() {
marker.stopMove();
}, false);
最终结果(截取部分):