riku
2024-02-27 7578c3ff65329b2269f099475eb687e963efac1c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function GridAnalysis(option) {
  this.gridView = []// 网格
  this.textView = []//文本
  this.opacity = 0.8//图形默认透明度
}
 
GridAnalysis.prototype = {
  /**
   * 绘制网格
   * @param points 网格线段坐标点组
   */
  drawGrid: function (points) {
    this.gridView = [];
    points.forEach((p) => {
      let line = MapUtil.parse2LngLat(p);
      var polyline = new AMap.Polyline({
        path: line,
        strokeStyle: 'solid',
        isOutline: false,
        borderWeight: 0,
        outlineColor: 'white',
        strokeWeight: 2, // 线条宽度,默认为 1
        strokeColor: '#CC0000', // 线条颜色
        lineJoin: 'round', // 折线拐点连接处样式
        showDir: true,
      });
      this.gridView.push(polyline);
    });
    MapUtil._map.add(this.gridView);
  },
 
  /**
   * 绘制网格风险图
   * @param {*} points
   */
  drawRectangle: function (points) {
    var that = this
    this.clear()
 
    var gpsList = []
    points.forEach((p) => {
      gpsList.push(p.lb, p.rt, p.c)
    })
    MapUtil.convertFromGPS(gpsList, function (gd) {
      for (let i = 0; i < points.length; i++) {
        const p = points[i];
        var lb = gd[i * 3]
        var rt = gd[i * 3 + 1]
        var c = gd[i * 3 + 2]
 
        var color = '#990D0D';
        switch (p.level) {
          case 0:
            color = '#990D0D';
            break;
          case 1:
            color = '#DBB70D';
            break;
          case 2:
            color = '#0DE707';
            break;
          default:
            color = '#0DE707';
            break;
        }
 
        let pList = MapUtil.parse2LngLat([lb, rt]);
        var bounds = new AMap.Bounds(...pList);
        var rectangle = new AMap.Rectangle({
          bounds: bounds,
          // strokeColor: '#ffffffff',
          strokeWeight: 0,
          strokeOpacity: 0,
          // strokeStyle还支持 solid
          strokeStyle: 'solid',
          fillColor: color,
          fillOpacity: that.opacity,
          cursor: 'pointer',
          zIndex: 50,
        });
 
        var text = new AMap.Text({
          text: p.value,
          anchor: 'center', // 设置文本标记锚点
          draggable: false,
          style: {
            'background-color': 'transparent',
            'border-width': 0,
            'text-align': 'center',
            'font-size': '12px',
            'color': 'white'
          },
          position: c
        });
        that.gridView.push(rectangle);
        that.textView.push(text);
      }
      MapUtil._map.add(that.gridView);
      that.toggleText(that.show)
    })
  },
  
  /**
   * 调整图形透明度
   * @param show 图形透明度调整
   */
  changeOpacity (show) {
    
    if (show) {
      this.opacity = 0.8
    } else {
      this.opacity = 0.3
    }
    this.gridView.forEach(g => {
      g.setOptions({
        fillOpacity: this.opacity,
      })
    });
  },
 
  /**
   * 切换文本显示
   */
  toggleText (show) {
    if (show) {
      this.show = true
      MapUtil._map.add(this.textView);
    } else {
      this.show = false
      MapUtil.removeViews(this.textView);
    }
  },
 
  clear() {
    MapUtil.removeViews(this.gridView);
    MapUtil.removeViews(this.textView);
    this.gridView = [];
    this.textView = [];
  },
};