vue根据站点A 与 站点B 之间的间距 实现小车(可能为多辆车)的动态移动

先不废话上效果图:

 

具体代码如下:

<template>
  <div>
    <h1>地铁线路实时动态图</h1>
    <div class="subway">
        <div class="lines">
            <div class="line_title" v-for="(item,index) in lines" :key="index">
                <div class="line_item">
                    {{item.lineName}}:
                </div>
            </div>
        </div>

        <div class="stations">
            <div class="station_title" v-for="(item,index) in stations" :key="index">
            <div class="station_item">
                {{item.stationName}}
            </div>
            <div class="line" v-if="index !== stations.length-1"></div>
            </div>
        </div>
        <div class="positions">
            <div class="act-point" v-for="(item,index) in positions" :style="{left: item.left + 'px', bottom: item.bottom + 'px'}" ></div>
        </div>
    </div>
  </div>
</template>

<script>
import { setInterval } from 'timers';
export default {
  data(){
    return {
      lines:[{lineName:'13号线'}],
      stations: [
        {stationName: '西直门'},
        {stationName: '大钟寺'},
        {stationName: '知春路'},
        {stationName: '五道口'},
        {stationName: '上地'},
        {stationName: '西二旗'},
        {stationName: '龙泽'}],
      positions: [
        {left: 1, bottom:20 ,name: '车01'},
        {left: 1, bottom:20 , name: '车02'},
        {left: 1,  bottom:20 ,name: '车03'},
        {left: 1,  bottom:20 ,name: '车04'}]
    }
  },
  methods: {
    calcPosition() {
      let arr = [ 0, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600];
    //   let arr1 = arr.reverse();
      let count = 0;
      setInterval(() => {
        if (count > arr.length -1) return 
        this.positions[0].left = arr[count];
        this.positions[1].left = arr[count + 1];
        this.positions[2].left = arr[count + 2];
        this.positions[3].left = arr[count + 3];
        count++
        console.log(this.positions[0].left)
      }, 3000)


    //   let count1 = 0;
    //   setInterval(() => {
    //     if (count > arr1.length -1) return 
    //     this.positions[0].bottom = -25
    //     this.positions[1].bottom = -25
    //     this.positions[2].bottom = -25
    //     this.positions[3].bottom = -25
    //     this.positions[0].left = arr1[count1];
    //     this.positions[1].left = arr1[count1 + 1];
    //     this.positions[2].left = arr1[count1 + 2];
    //     this.positions[3].left = arr1[count1 + 3];
    //     count++
    //     console.log(this.positions[0].left)
    //   }, 3000)


    }
  },
  mounted(){
    this.calcPosition()
  }
}
</script>

<style type="text/css">
    .subway {
        margin-top: 20px;
        position: relative;
    }
    /* 线路 */
    .subway .lines .line_item{
        margin-bottom:10px;
        font-size:16px;
        color:#000;
        font-weight:bold;
    }
    /* 车站 */
    .subway .station_title {
        display: inline-block;
        width: 100px;
        /* border-bottom: 4px solid #000; */
    }
    .subway .station_item {
        font-size: 12px;
        margin-bottom: 40px;
        word-wrap: break-word;
        word-break: break-all;
        overflow: hidden;
    }
    .subway .line {
        position: relative;
        width: 100px;
        border-bottom: 4px solid #000;
        /* border-radius: 4px; */
        margin-top: 15px;
        margin-left:10px;
    }
    .subway .line::after {
        position: absolute;
        left: -2px;
        bottom: -10px;
        content: '';
        display: inline-block;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: #0089ff;
        opacity: 1;
    }
    .subway .station_title:nth-last-child(2) .line::before{
        position: absolute;
        right: -2px;
        bottom: -10px;
        content: '';
        display: inline-block;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: #0089ff;
        opacity: 1;
    }
    .subway .line::after :hover {
        bottom: -8px;
        width: 12px;
        height: 12px;
        opacity: 1;
    }
    /* 小车 */
    .positions .act-point {
        position: absolute;
        bottom: 20px;
        left: -6px;
        width: 50px;
        height: 12px;
        /* border-radius: 50%; */
        background: url('/static/img/tain2.png') no-repeat;
    }
</style>

 

最后为了方便大家的沟通与交流请加QQ群: 625787746

请进QQ群交流:【IT博客技术分享群①】:https://jq.qq.com/?_wv=1027&k=DceI0140

实现多辆小车在高德地图上移动,可以使用 Vue.js 结合高德地图 JavaScript API,为每辆小车创建一个独立的标记,并对每个标记进行移动操作。 以下是一个示例代码: ```html <template> <div> <div id="map" style="width: 800px; height: 600px;"></div> </div> </template> <script> export default { data() { return { map: null, markers: [ { id: 1, lnglat: [116.39, 39.9] }, { id: 2, lnglat: [116.4, 39.91] }, { id: 3, lnglat: [116.41, 39.92] }, // ... ] }; }, mounted() { // 创建地图 this.map = new AMap.Map('map', { zoom: 15, center: [116.39, 39.9] }); // 创建小车标记 this.markers.forEach(markerData => { const marker = new AMap.Marker({ map: this.map, position: markerData.lnglat, icon: new AMap.Icon({ size: new AMap.Size(50, 50), image: 'https://webapi.amap.com/images/car.png', imageSize: new AMap.Size(50, 50) }) }); // 设置小车标记的角度 function setMarkerAngle(angle) { marker.setAngle(angle); } // 设置小车标记的位置 function setMarkerPosition(position) { marker.setPosition(position); } // 获取小车标记的角度 function getAngle(position) { // 根据当前位置和下一个位置计算角度 // ... } // 小车移动动画 function move() { let index = 0; setInterval(() => { const currentPosition = markerData.lnglat; const nextPosition = this.markers[index].lnglat; const angle = getAngle(currentPosition, nextPosition); setMarkerAngle(angle); setMarkerPosition(nextPosition); index++; if (index >= this.markers.length) { index = 0; } }, 1000); } // 开始小车移动 move(); }); } } </script> ``` 在上述代码中,通过遍历 `markers` 数组为每辆小车创建一个独立的标记,并在 `mounted` 钩子函数中循环地对每个标记进行移动操作。你可以根据实际需求修改标记的初始位置、路径、移动速度等参数。记得在 `mounted` 钩子函数中调用相关方法来初始化地图和小车移动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT博客技术分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值