最近收到了一个需求,需要在vue-amap上做轨迹,之前是想着连成线,但由于固件提供的数据无法支持连成线,所以就只能做成动画的轨迹点。
我们先来看一下效果图:
视频动态效果地址如下: 链接: 动画视频
一:引入vue-amap 组件
<el-amap
:center="center"
:amap-manager="amapManager"
:zoom="zoom"
:plugins="plugins"
:events="events"
:resize-enable="true"
:map-style="mapStyle"
class="map"
>
<el-amap-marker
v-for="(marker,index) in markers"
:key="'marker'+index"
:position="marker.position"
:events="marker.events"
:content="marker.icon"
/>
</el-amap>
其实动画中的点动起来其实是markers 的改变和添加这种思路。
二:模拟数据
startMove() {
let markers = []
for (let i = 0; i < 20; i++) {
markers.push({
position: [121.621 + Math.random() * 0.001, 31.2444 + 0.001 * Math.random()],
events: {},
// <div style='color:#fff;position: absolute;top: -20px;font-size: 10px;left: -25px;width:85px'>停留:${
// Number(item.duration) <= 60
// ? item.duration + '秒'
// : item.duration / 3600 >= 1
// ? (item.duration / 3600).toFixed(2) + '小时'
// : (item.duration / 60).toFixed(2) + '分钟'
// }</div>
icon: `<div style='display: inline-block;text-align: center;position: relative;' class='animate__animated animate__pulse animate__infinite'>
<div style='position: absolute;top: -25px;font-size: 10px;left: -110px;width: 275px;color:#fff'>日期:${this.changeTime(
1678273262229 + i * 10000
)}</div>
<div style='color:#fff;position: absolute;top:2px;left:5px;font-size: 10px;'>${i + 1}</div>
<div class="animate__animated animate__pulse animate__infinite" style="background:url(https://images.hicling.com:9443/weixin/normalBg.png) no-repeat;background-size:contain ;width:40px;height:50px;text-align:center;padding-top: 5px;"><img style="width:30px;height:30px;border-radius:50%" src=${
this.avatar
}
></div>
</div>`
})
}
this.$refs.trajectory.copyMarkersList = markers
setTimeout(() => {
this.$refs.trajectory.zoom = 20
this.$refs.trajectory.markers = []
this.$refs.trajectory.controlFlag = true
this.$refs.trajectory.copyMarkersList = markers
this.$refs.trajectory.againDonghua()
}, 500)
}
这里的 copyMarkersList 是所有的轨迹点。
三:轨迹点动起来
这个就是让动画动起来的核心,使用setInterval 的方式,这样就可以使轨迹动起来。
四:添加轨迹控制器
五:总结
如需项目demo,微信扫下面的二维码,注册成功,到下面的我的-联系客服 问我要代码