Mapbox添加动态线条

// 添加多条路线
    addMultiLineString(data) {
      mapboxMap.map.getLayer("multiLineLayer") &&
        mapboxMap.map.removeLayer("multiLineLayer");
      mapboxMap.map.getSource("multiLineSource") &&
        mapboxMap.map.removeSource("multiLineSource");
      let features = [];
      let linerArr = [];
      for (let index = 0; index < data.length; index++) {
        let color = "";
        if (data[index].channelType === "51") {
          color = "#92F7F4";
        } else if (data[index].channelType === "52") {
          color = "#70B603";
        } else if (data[index].channelType === "53") {
          color = "#EC808D";
        } else if (data[index].channelType === "54") {
          color = "#1195db";
        } else if (data[index].channelType === "55") {
          color = "#f4ea29";
        }
        features.push({
          type: "Feature",
          id: index,
          geometry: JSON.parse(data[index].geom),
          properties: {
            ...data[index],
            color: color,
          },
        });
        linerArr.push(JSON.parse(data[index].geom));
      }
      mapboxMap.map.addSource("line", {
        type: "geojson",
        data: {
          type: "FeatureCollection",
          features: features,
        },
      });
      mapboxMap.map.addLayer({
        type: "line",
        source: "line",
        id: "multiLineLayer",
        paint: {
          "line-color": ["get", "color"],
          "line-width": 6,
        },
      });
      mapboxMap.map.addLayer({
        type: "line",
        source: "line",
        id: "line-dashed",
        paint: {
          "line-color": "white",
          "line-width": 2,
          "line-dasharray": [0, 3, 3],
        },
      });
      const dashArraySequence = [
        [0, 3, 3],
        [0.5, 4, 2.5],
        [1, 4, 2],
        [1.5, 4, 1.5],
        [2, 4, 1],
        [2.5, 4, 0.5],
        [3, 4, 0],
        [0, 0.5, 3, 3.5],
        [0, 1, 3, 3],
        [0, 1.5, 3, 2.5],
        [0, 2, 3, 2],
        [0, 2.5, 3, 1.5],
        [0, 3, 3, 1],
        [0, 3.5, 3, 0.5],
      ];
      let step = 0;
      function animateDashArray(timestamp) {
        // Update line-dasharray using the next value in dashArraySequence. The
        // divisor in the expression `timestamp / 50` controls the animation speed.
        const newStep = parseInt((timestamp / 50) % dashArraySequence.length);

        if (newStep !== step) {
          mapboxMap.map.setPaintProperty(
            "line-dashed",
            "line-dasharray",
            dashArraySequence[step]
          );
          step = newStep;
        }

        // Request the next frame of the animation.
        requestAnimationFrame(animateDashArray);
      }

      // start the animation
      animateDashArray(0);
      this.addArrowIcon(features);
      mapboxMap.map.off("click", "multiLineLayer", this.popupEvent);
      mapboxMap.map.on("click", "multiLineLayer", this.popupEvent);
    },
// 添加箭头
    addArrowIcon(features) {
      if (mapboxMap.map.getSource("icon")) {
        mapboxMap.map.removeLayer("arrowLayer");
        mapboxMap.map.removeSource("icon");
        mapboxMap.map.removeImage("arrow");
      }
      mapboxMap.map.loadImage(
        require("../../../../../static/img/arrorw.png"),
        (error, image) => {
          if (error) throw error;
          mapboxMap.map.addImage("arrow", image);
          mapboxMap.map.addSource("icon", {
            type: "geojson",
            data: {
              type: "FeatureCollection",
              features,
            }, // 轨迹geojson格式数据
          });
          mapboxMap.map.addLayer({
            id: "arrowLayer",
            type: "symbol",
            source: "icon",
            layout: {
              "symbol-placement": "line",
              "symbol-spacing": 1, // 图标间隔,默认为250
              "icon-image": "arrow", // 箭头图标
              "icon-size": 0.3,
            },
          });
        }
      );
    },

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值