vue 带航向(方向箭头的)实时轨迹线路实现(canvas简单真实!)

下述点位可以通过websocket实时推送真实的points;我的箭头是用黑色三角形代替的(可自定义)

<template>
  <div>
    <canvas ref="canvas" width="800" height="600"></canvas>
  </div>
</template>

<script>
export default {
  mounted() {
    this.canvas = this.$refs.canvas;
    this.ctx = this.canvas.getContext("2d");

    const points = [];
    for (let i = 0; i < 10; i++) {
      const x = Math.random() * this.canvas.width;
      const y = Math.random() * this.canvas.height;
      points.push({ x, y });
    }

    points.sort((a, b) => a.x - b.x);

    this.draw(points);
  },
  methods: {
    draw(points) {
      const ctx = this.ctx;
      const arrowInterval = 50; // 箭头间隔距离

      ctx.lineWidth = 15; // 设置线段宽度为15px
      ctx.strokeStyle = "green"; // 设置线段颜色为绿色

      // 绘制线段
      ctx.beginPath();
      ctx.moveTo(points[0].x, points[0].y);
      for (let i = 1; i < points.length; i++) {
        ctx.lineTo(points[i].x, points[i].y);
      }
      ctx.stroke();

      // 绘制箭头
      ctx.fillStyle = "black"; // 设置箭头颜色为黑色
      let totalDistance = 0; // 累计距离
      for (let i = 1; i < points.length; i++) {
        const dx = points[i].x - points[i - 1].x;
        const dy = points[i].y - points[i - 1].y;
        const segmentDistance = Math.sqrt(dx * dx + dy * dy);

        totalDistance += segmentDistance;

        while (totalDistance >= arrowInterval) {
          const ratio = (totalDistance - arrowInterval) / segmentDistance;
          const arrowX = points[i - 1].x + dx * ratio;
          const arrowY = points[i - 1].y + dy * ratio;

          ctx.save();
          ctx.translate(arrowX, arrowY);
          ctx.rotate(Math.atan2(dy, dx));

          ctx.beginPath();
          ctx.moveTo(0, -5);
          ctx.lineTo(10, 0);
          ctx.lineTo(0, 5);
          ctx.closePath();
          ctx.fill();

          ctx.restore();

          totalDistance -= arrowInterval;
        }
      }
    }


  },
};
</script>

<style>
canvas {
  border: 1px solid #000;
}
</style>

上图:

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
实时路线轨迹实现需要结合高德地图的定位功能和绘制功能。 1. 首先,在Vue中引入高德地图的JavaScript API,并创建地图对象: ```javascript import AMapLoader from '@amap/amap-jsapi-loader' // 创建地图对象 let map = null AMapLoader.load({ key: 'your amap key', version: '2.0', plugins: ['AMap.Geolocation'] }).then((AMap) => { map = new AMap.Map('map-container') }) ``` 2. 调用高德地图的定位功能获取当前位置: ```javascript let geolocation = null // 创建定位对象 AMapLoader.load({ key: 'your amap key', version: '2.0', plugins: ['AMap.Geolocation'] }).then((AMap) => { geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为false timeout: 10000, // 超过10秒后停止定位,默认为无穷大 buttonOffset: new AMap.Pixel(10, 20), // 定位按钮的位置偏移量 }) map.addControl(geolocation) // 获取当前位置 geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 定位成功,将地图中心设置为当前位置 map.setCenter(result.position) } else { // 定位失败 console.log('定位失败', result.message) } }) }) ``` 3. 在地图上绘制路线轨迹: ```javascript let path = [] // 路线轨迹点集合 // 添加路线轨迹 let polyline = new AMap.Polyline({ path, strokeColor: '#00a1fe', strokeWeight: 5, }) polyline.setMap(map) // 更新路线轨迹 function updatePath(newPoint) { path.push(newPoint) polyline.setPath(path) } ``` 4. 实时更新路线轨迹: ```javascript // 开启定时器,每隔1秒更新路线轨迹 setInterval(() => { // 获取当前位置 geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 定位成功,更新路线轨迹 updatePath(result.position) } else { // 定位失败 console.log('定位失败', result.message) } }) }, 1000) ``` 以上就是Vue高德地图实现实时路线轨迹的基本流程,需要注意的是,高德地图的JavaScript API使用了Promise异步机制,需要注意回调函数的执行时机。另外,在实际应用中,还需要结合后端接口获取历史路线轨迹数据,并将其加载到地图上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

憨子周

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

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

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

打赏作者

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

抵扣说明:

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

余额充值