基于 Vue3.0、Vite、 Ant-Design-Vue、TypeScript、tdesign-vue-next 高德地图的轨迹回放,支持倍速及进度条拖拽

1.效果图

2.注册账号,获取Key,安装依赖

pnpm add @amap/amap-jsapi-loader --registry=https://registry.npmmirror.com
pnpm add tdesign-vue-next --registry=https://registry.npmmirror.com

3.代码实现

<template>
  <div id="container" ref="mapRef" class="map-container h-full"></div>
  <div class="absolute handler-bar flex items-center px-30 justify-between">
    <img
      v-if="animationStatus"
      src="@/assets/images/push.png"
      class="action-icon cursor-pointer"
      @click="changeStatus"
    />
    <img
      v-else
      src="@/assets/images/start.png"
      class="action-icon cursor-pointer"
      @click="changeStatus"
    />
    <div
      v-for="(item, index) in speedList"
      :key="`speed-${index}`"
      class="speed mr-10 cursor-pointer"
      :class="speed === item.value ? 'active' : ''"
      @click="changeSpeed(item.value)"
    >
      {{ item.label }}
    </div>
    <Slider
      class="t-slider"
      v-model="sliderValue"
      :disabled="!initPassed.length"
      :tooltip-props="false"
      @mousedown="changeMouseStatus('down')"
      @mouseup="changeMouseStatus('up')"
      @change="sliderChange"
      @click="sliderClick"
    />
  </div>
</template>
<script setup lang="ts">
  import { ref, onMounted, shallowRef } from 'vue';
  import AMapLoader from '@amap/amap-jsapi-loader';
  import { MessagePlugin, Slider } from 'tdesign-vue-next';

  const map = shallowRef(null);
  const marker = ref();
  const speedList = [
    {
      label: '1.0x',
      value: 1,
    },
    {
      label: '2.0x',
      value: 2,
    },
    {
      label: '3.0x',
      value: 3,
    },
  ];
  const mouseDown = ref(false);
  const clickMove = ref(false);
  const visible = ref(false);
  const sliderValue = ref(0);
  const speed = ref(1);
  const animationStatus = ref(false);
  const initPassed = ref([
    [116.478935, 39.997761],
    [116.478939, 39.997825],
    [116.478912, 39.998549],
    [116.478912, 39.998549],
    [116.478998, 39.998555],
    [116.478998, 39.998555],
    [116.479282, 39.99856],
    [116.479658, 39.998528],
    [116.480151, 39.998453],
    [116.480784, 39.998302],
    [116.480784, 39.998302],
    [116.481149, 39.998184],
    [116.481573, 39.997997],
    [116.481863, 39.997846],
    [116.482072, 39.997718],
    [116.482362, 39.997718],
    [116.483633, 39.998935],
    [116.48367, 39.998968],
    [116.484648, 39.999861],
  ]);
  const stopValue = ref(0);
  const passedArr = ref([]);

  const changeMouseStatus = (mouseStatus) => {
    if (mouseStatus === 'down') {
      mouseDown.value = true;
      pauseAnimation();
    }
    if (mouseStatus === 'up') {
      mouseDown.value = false;
      if (animationStatus.value) {
        changeStatus('continue');
      }
    }
  };
  const changeStatus = (type) => {
    if (!initPassed.value || !initPassed.value.length) {
      MessagePlugin.warning('实际巡检轨迹为空!');
      return;
    }
    if (type !== 'continue') {
      animationStatus.value = !animationStatus.value;
    }
    if (sliderValue.value === 100) {
      sliderValue.value = 0;
    }
    stopValue.value = sliderValue.value;
    const index = Math.ceil(initPassed.value.length * (sliderValue.value / 100));
    if (index === 0) {
      passedArr.value = [...initPassed.value];
    } else {
      passedArr.value = initPassed.value.slice(index);
    }
    if (animationStatus.value) {
      setTimeout(() => {
        startAnimation();
      });
    } else {
      pauseAnimation();
    }
  };
  const moveend = (info) => {
    const { index } = info;
    clickMove.value = false;
    if (index === passedArr.value.length - 1) {
      sliderValue.value = 100;
      animationStatus.value = false;
    }
  };
  const movingInfo = (info) => {
    if (!mouseDown.value && !clickMove.value) {
      const { index, progress } = info;
      sliderValue.value = stopValue.value + ((index + progress) / initPassed.value.length) * 100;
      if (sliderValue.value === 100) {
        animationStatus.value = false;
      }
    }
  };
  const sliderClick = () => {
    const index = Math.ceil(initPassed.value.length * (sliderValue.value / 100));
    const lnglat = initPassed.value[index];
    clickMove.value = true;
    animationTo(lnglat);
    setTimeout(() => {
      if (animationStatus.value) {
        changeStatus('continue');
      }
    });
  };
  const sliderChange = (val) => {
    if (mouseDown.value) {
      const index = Math.ceil(initPassed.value.length * (val / 100));
      const lnglat = initPassed.value[index];
      sliderValue.value = val;
      animationTo(lnglat);
    }
  };
  const onCancel = () => {
    stopAnimation();
    animationStatus.value = false;
  };
  const stopAnimation = () => {
    marker.value.stopMove();
  };
  const changeSpeed = (val) => {
    speed.value = val;
    pauseAnimation();
    setTimeout(() => {
      if (animationStatus.value) {
        changeStatus('continue');
      }
    });
  };
  const open = () => {
    visible.value = true;
  };
  const initMap = () => {
    AMapLoader.load({
      key: 'YOUR_KEY',
      version: '2.0',
      plugins: ['AMap.MoveAnimation'],
    })
      .then((AMap) => {
        map.value = new AMap.Map('container', {
          pitch: 50,
          viewMode: '3D',
          terrain: true,
          resizeEnable: true,
          center: initPassed.value[0],
          zoom: 17,
          mapStyle: 'amap://styles/normal',
        });
        marker.value = new AMap.Marker({
          map: map.value,
          position: initPassed.value[0],
          icon: 'https://webapi.amap.com/images/car.png',
          offset: new AMap.Pixel(-26, -13),
          autoRotation: true,
          angle: -90,
        });
        marker.value.setMap(map.value);
        const passedPolyline = new AMap.Polyline({
          map: map.value,
          path: initPassed.value,
          strokeColor: '#AF5',
          strokeOpacity: 1,
          strokeWeight: 6,
        });
        const passedPolylineInit = new AMap.Polyline({
          map: map.value,
          path: initPassed.value,
          showDir: true,
          strokeColor: '#28F',
          strokeWeight: 6,
          zIndex: 97,
        });
        passedPolylineInit.setMap(map.value);
        marker.value.on('moving', (e) => {
          movingInfo(e);
          passedPolyline.setPath(e.passedPath);
          map.value.setCenter(e.target.getPosition(), true);
        });
        marker.value.on('moveend', (e) => {
          moveend(e);
        });
      })
      .catch((e) => {
        console.log(e);
      });
  };
  const startAnimation = () => {
    marker.value.moveAlong(passedArr.value, {
      duration: 1000 / speed.value,
      autoRotation: true,
    });
  };
  const animationTo = (target) => {
    marker.value.moveTo(target, {
      duration: 0,
      autoRotation: true,
    });
  };
  const pauseAnimation = () => {
    marker.value.pauseMove();
  };
  onMounted(() => {
    initMap();
  });
  defineExpose({ open });
</script>
<style lang="less" scoped>
  .h600 {
    height: calc(100vh - 80px);
  }
  .handler-bar {
    right: 30px;
    bottom: 20px;
    z-index: 1;
    width: 690px;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 30px;
    height: 53px;
    .action-icon {
      width: 32px;
      height: 32px;
      margin-right: 40px;
    }
    .speed {
      background: #dcdcdc;
      border-radius: 4px;
      height: 32px;
      line-height: 32px;
      width: 52px;
      text-align: center;
      color: rgba(0, 0, 0, 0.6);
      font-size: 18px;
    }
    .active {
      background: #0052d9;
      color: #fff;
    }
  }
  .color-bray {
    color: rgba(0, 0, 0, 0.6);
  }
  .f12 {
    font-size: 12px;
  }
  .color-check {
    color: #0052d9;
  }
</style>

特别感谢:@亦非余

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 + TypeScript + Vite 中使用 vue-draggable-next 插件实现按钮在区域内任意移动的功能,你可以按照以下步骤进行操作: 1. 确保你已经安装了 Vue 3、TypeScriptVite,并且已经创建了一个 Vue 3 + TypeScript + Vite 的项目。 2. 在项目根目录下,通过命令行安装 vue-draggable-next: ```bash npm install vue-draggable-next ``` 3. 在你的 Vue 组件中,首先导入所需的库和类型: ```typescript import { defineComponent, ref } from 'vue'; import { draggable } from 'vue-draggable-next'; ``` 4. 创建一个包含按钮的区域容器,并在容器内部使用 `draggable` 组件来包裹按钮。将 `v-model` 指令绑定到按钮的位置属性。 ```typescript export default defineComponent({ components: { draggable }, setup() { const buttonPosition = ref({ x: 0, y: 0 }); return { buttonPosition }; } }); ``` 5. 在模板中使用 `draggable` 组件和按钮。 ```html <template> <div class="container"> <draggable v-model="buttonPosition" :bounds="containerBounds"> <button class="button">移动按钮</button> </draggable> </div> </template> ``` 6. 添加样式,确保容器具有适当的宽度、高度和边界。 ```html <style> .container { position: relative; width: 500px; height: 500px; border: 1px solid #ccc; } .button { position: absolute; left: 0; top: 0; } </style> ``` 通过以上步骤,你就可以在 Vue 3 + TypeScript + Vite 中使用 vue-draggable-next 插件来实现按钮在区域内任意移动的功能。这样,按钮的位置将随着拖动而改变,并受到容器边界的限制。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值