Blame view

src/components/inspection/inspectionTaskMap.vue 5.77 KB
b25b036d   wuxw   v1.9 优化日期
1
  <template>
48ea9c43   wuxw   巡检开发完成
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
    <div class="inspection-task-map-container">
      <div id="inspectionTaskMap" style="width: 100%; height: 600px;"></div>
    </div>
  </template>
  
  <script>
  import { queryInspectionTaskDetail } from '@/api/inspection/inspectionPointApi'
  import { getCommunityId } from '@/api/community/communityApi'
  
  export default {
    name: 'InspectionTaskMap',
    data() {
      return {
        map: null,
        communityId: '',
        taskPoints: [],
        markerLayer: null,
        labelLayer: null,
        polylineLayer: null
      }
    },
    mounted() {
      this.communityId = getCommunityId()
      // 延迟初始化地图,确保DOM已经渲染
    },
    methods: {
      loadData(task) {
57c370b2   wuxw   v1.9 优化巡检地图展示问题
29
        console.log('loadData',task)
48ea9c43   wuxw   巡检开发完成
30
31
32
33
        this.initMap()
        this.loadTask(task)
      },
      initMap() {
57c370b2   wuxw   v1.9 优化巡检地图展示问题
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        // 若已有地图或图层,先清理避免重复初始化
        if (this.markerLayer) {
          this.markerLayer.setMap(null)
          this.markerLayer = null
        }
        if (this.labelLayer) {
          this.labelLayer.setMap(null)
          this.labelLayer = null
        }
        if (this.polylineLayer) {
          this.polylineLayer.setMap(null)
          this.polylineLayer = null
        }
        if (this.map) {
          this.map.destroy()
          this.map = null
        }
        console.log('initMap',this.map)
  
48ea9c43   wuxw   巡检开发完成
53
54
        // 默认中心点(北京)
        const center = new window.TMap.LatLng(39.916527, 116.397128)
57c370b2   wuxw   v1.9 优化巡检地图展示问题
55
        console.log('center',center)
48ea9c43   wuxw   巡检开发完成
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  
        // 创建地图实例
        this.map = new window.TMap.Map(document.getElementById('inspectionTaskMap'), {
          center: center,
          zoom: 13,
          baseMap: {
            type: 'vector',
            features: ['base', 'building2d', 'point']
          }
        })
      },
  
      async loadTask(task) {
        if (!task || !task.taskId) return
  
        try {
          const params = {
            communityId: this.communityId,
            taskId: task.taskId,
            page: 1,
            row: 1000
          }
  
          const response = await queryInspectionTaskDetail(params)
          this.taskPoints = response.data
          this.addPointsToMap(this.taskPoints)
        } catch (error) {
          console.error('获取任务点失败:', error)
          this.$message.error(this.$t('inspectionTaskMap.fetchError'))
        }
      },
  
      addPointsToMap(points) {
        if (!this.map || !points || points.length === 0) return
  
        // 清除之前的标记
        if (this.markerLayer) this.markerLayer.setMap(null)
        if (this.labelLayer) this.labelLayer.setMap(null)
        if (this.polylineLayer) this.polylineLayer.setMap(null)
  
        const geometriesMarker = []
        const geometriesLabel = []
        const path = []
  
        points.forEach(point => {
          if (!point.lat || !point.lng) return
  
          const position = new window.TMap.LatLng(point.lat, point.lng)
  
          // 确定状态对应的样式
          const markerStyle = point.state === '20200405' ? 'myStyle_red' : 'myStyle'
          const labelStyle = point.state === '20200405' ? 'label_red' : 'label'
  
          // 添加标记
          geometriesMarker.push({
            id: point.inspectionId,
            styleId: markerStyle,
            position: position
          })
  
          // 添加标签
          geometriesLabel.push({
            id: `label_${point.inspectionId}`,
            styleId: labelStyle,
            position: position,
            content: point.inspectionName
          })
  
          // 添加路径点
          path.push(position)
        })
  
        // 创建标记图层
        this.markerLayer = new window.TMap.MultiMarker({
          map: this.map,
          styles: {
            "myStyle": new window.TMap.MarkerStyle({
              width: 25,
              height: 35,
              src: '/img/inspection.png',
              anchor: { x: 32, y: 32 }
            }),
            "myStyle_red": new window.TMap.MarkerStyle({
              width: 25,
              height: 35,
              src: '/img/inspection_do.png',
              anchor: { x: 32, y: 32 }
            })
          },
          geometries: geometriesMarker
        })
  
        // 创建标签图层
        this.labelLayer = new window.TMap.MultiLabel({
          map: this.map,
          styles: {
            'label': new window.TMap.LabelStyle({
              color: '#3777FF',
              size: 20,
              offset: { x: 0, y: 15 },
              angle: 0,
              alignment: 'center',
              verticalAlignment: 'middle'
            }),
            'label_red': new window.TMap.LabelStyle({
              color: '#CC0000',
              size: 20,
              offset: { x: 0, y: 15 },
              angle: 0,
              alignment: 'center',
              verticalAlignment: 'middle'
            })
          },
          geometries: geometriesLabel
        })
  
        // 创建路径图层
        this.polylineLayer = new window.TMap.MultiPolyline({
          map: this.map,
          styles: {
            'style_blue': new window.TMap.PolylineStyle({
              color: '#3777FF',
              width: 6,
              borderWidth: 5,
              borderColor: '#FFF',
              lineCap: 'butt'
            }),
            'style_red': new window.TMap.PolylineStyle({
              color: '#CC0000',
              width: 6,
              borderWidth: 5,
              borderColor: '#FFF',
              lineCap: 'butt'
            })
          },
          geometries: [
            {
              id: 'pl_1',
              styleId: 'style_blue',
              paths: path
            },
            {
              id: 'pl_2',
              styleId: 'style_red',
              paths: path
            }
          ]
        })
  
        // 如果有路径点,调整地图中心点和缩放级别
        if (path.length > 0) {
          this.map.setCenter(path[0])
          this.map.setZoom(16)
        }
      }
    },
    beforeDestroy() {
      // 组件销毁时清除地图实例
      if (this.map) {
        this.map.destroy()
        this.map = null
      }
    }
  }
  </script>
  
  <style scoped>
  .inspection-task-map-container {
    width: 100%;
    height: 100%;
  }
  </style>