Riku
2025-06-11 5679cbbb630092a197d991cb41997a2d953261e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { map } from './index_old';
import calculate from './calculate';
import { getHexColor } from '../color';
 
var defaultPolylineArr = [];
const lineMap = new Map();
 
function newPolyline(path, color) {
  // eslint-disable-next-line no-undef
  return new AMap.Polyline({
    path: path,
    // strokeStyle: 'solid',
    strokeStyle: 'dashed',
    isOutline: true,
    borderWeight: 1,
    outlineColor: 'white',
    strokeWeight: 4, // 线条宽度,默认为 1
    strokeColor: color, // 线条颜色
    lineJoin: 'round', // 折线拐点连接处样式
    showDir: true
  });
}
 
function drawDirection(path) {
  const polyline = newPolyline(path, '#ffd82a');
  map.add(polyline);
  return polyline;
}
 
export default {
  drawLine(fDatas, factor) {
    if (defaultPolylineArr.length > 0) {
      map.remove(defaultPolylineArr);
      defaultPolylineArr = [];
    }
 
    const lnglats_GD = fDatas.lnglats_GD;
    const colors = factor.colors;
 
    // this.hideLine();
 
    var path = calculate.parse2LngLat(lnglats_GD);
 
    let sIndex = 0;
    for (let i = 1; i < path.length; i++) {
      var r = lnglats_GD[i];
      var lastP = lnglats_GD[i - 1];
      // eslint-disable-next-line no-undef
      var distance = AMap.GeometryUtil.distance(r, lastP);
      const c = colors[i];
      const lastC = colors[i - 1];
      if (distance > 500 || c != lastC) {
        let _path, _color;
        _path = path.slice(sIndex, i);
        _color = getHexColor(
          lastC.map((v, index) => {
            if (index < lastC.length - 1) {
              return v * 255;
            } else {
              return v;
            }
          })
        );
        // 当两点距离超过500时,认为两点不连续,不绘制连线
        if (distance > 500) {
          sIndex = i;
        } else {
          sIndex = i - 1;
        }
 
        // 创建折线实例
        const polyline = newPolyline(_path, _color);
        defaultPolylineArr.push(polyline);
      }
    }
    if (sIndex < path.length - 1) {
      const c = colors[path.length - 1];
      const _path = path.slice(sIndex, path.length);
      const _color = getHexColor(
        c.map((v, index) => {
          if (index < c.length - 1) {
            return v * 255;
          } else {
            return v;
          }
        })
      );
      const polyline = newPolyline(_path, _color);
      defaultPolylineArr.push(polyline);
    }
    // 将折线添加至地图实例
    map.add(defaultPolylineArr);
    return defaultPolylineArr;
  },
 
  drawTagLine(tag, fDatas, factor) {
    if (lineMap.has(tag)) {
      const _polylineArr = lineMap.get(tag);
      map.add(_polylineArr);
    } else {
      const _polylineArr = this.drawLine(fDatas, factor);
      lineMap.set(tag, _polylineArr);
    }
  },
 
  hideLine(tag) {
    if (tag && lineMap.has(tag)) {
      const _polylineArr = lineMap.get(tag);
      map.remove(_polylineArr);
    } else {
      lineMap.forEach((v) => {
        map.remove(v);
      });
    }
  },
 
  drawDirection
};