liuxiaolong
2019-05-06 acc45d2ece02563964d7b8a22ff0c40bd8358889
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
  <template>
    <div class="height100 pr overflow-hidden">
      <div :id="newMapId" class="height100"></div>
    </div>
  </template>
 
<script>
/* eslint-disable */
let settingMapHtml = `<div class="pl10 pt20 pr10 pb20">
          <h3 class="pl30 pb20">区域信息</h3>
          <div class="el-form settingMap-text" >
            <div class="el-form-item">
              <label class="el-form-item__label" style="width: 100px;">区域名称 :</label>
              <div class="el-form-item__content" style="margin-left: 100px;">
                <div class="el-input">
                  <input id="settingMapName" autocomplete="off" type="text" rows="2" class="el-input__inner">
                </div>
              </div>
            </div>
          </div>
          <div class="pt20 pb20 text-center">
            <button type="button" id="settingMapInfoBtn" class="el-button el-button--primary pl30 pr30">
              <span>立即创建</span>
            </button>
          </div>
        </div>`
import { findInArrJson } from '@/components/common/util.js'
import { getSchoolInfo } from '@/server/home.js'
import { getDistrictAllSchoolById } from '@/server/common.js'
export default {
  props: [
    'newId', //自定义id 页面多次使用会用到
    'defaultMapZoom', //自定义地图初始层级
    
    'searchArr',
 
    'isCamerasBackfn', //是否显示摄像机。摄像机回调函数
    'panToVal', //自动定位  {lng:"val",lat:"val"}
    'alarmInfoData', //报警信息数据 需要自处里,具体看home.vue
    'activeDeviceIds', //传入选中设备ids  设置高亮
    'removeActiveAlarmInfo', //删除选中状态   字符串时间戳
 
    'modelUpdate',
    'addressStr', //地址名称
    'showSeachModel' //是否启动搜索功能
  ],
  data() {
    return {
      userInfo: this.$store.getters.basicUserInfo,
      countNum: null,
 
      bMapObj: {},
      areaData: [],
      deviceList: [],
 
      alarmInfoListActive: '',
      deviceTypeList: [],
 
      cameraMarkers: [],
      cameraDensitys: [],
      cameraAreas: [],
      textInfoWindow: {},
    }
  },
  computed: {
    newMapId() {
      return this.newId && this.newId !== '' ? this.newId : 'mapDynamics'
    },
    newDefaultMapZoom() {
      return this.defaultMapZoom && this.defaultMapZoom !== ''
        ? this.defaultMapZoom
        : 12
    },
    newAlarmInfoList() {
      return this.alarmInfoData
    }
  },
  methods: {
    /* 查询地图数据 */
    async getMapSchoolList() {
      let res = await getDistrictAllSchoolById({
        currentId: this.userInfo.orgId
      })
      if (res && res.data) {
        this.deviceList = res.data
      }
    },
    /* 查询学校详情 */
    async getSchoolInfoData(id) {
      let res = await getSchoolInfo({ id })
      if (res) {
        this.countNum = res
      }
    },
 
    /* 地图初始化 */
    bmapinit() {
      /*地图init*/
      this.bMapObj = new BMap.Map(this.newMapId, {
        enableMapClick: false,
        minZoom: 14
      })
      let point = new BMap.Point(116.331398, 39.897445) //初始化位置(116.331398,39.897445);
      this.bMapObj.centerAndZoom(point, this.newDefaultMapZoom)
      // this.bMapObj.setMapStyle({ style: 'midnight' });              //设置主题
      let bottom_right_navigation = new BMap.NavigationControl({
        anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
        type: BMAP_NAVIGATION_CONTROL_ZOOM
      }) //右上角,仅包含平移和缩放按钮
      this.bMapObj.addControl(bottom_right_navigation) //添加控件
      this.bMapObj.addControl(new BMap.MapTypeControl()) //this.bMapObj.
      this.bMapObj.disable3DBuilding()
      /*缩放控件type有四种类型:
          BMAP_NAVIGATION_CONTROL_SMALL:仅包含平移和缩放按钮;
          BMAP_NAVIGATION_CONTROL_PAN:仅包含平移按钮;
          BMAP_NAVIGATION_CONTROL_ZOOM:仅包含缩放按钮*/
      this.bMapObj.enableScrollWheelZoom() //启用滚轮放大缩小
 
      //创建全局弹窗
      this.textInfoWindow = new BMap.InfoWindow('', {
        width: 450,
        height: 250,
        offset: new BMap.Size(0, -10)
      })
    },
    /*城市ip定位*/
    bmaSetCenterByIp() {
      const myCity = new BMap.LocalCity()
      myCity.get(result => {
        var cityName = result.name
        this.bMapObj.setCenter(cityName)
      })
    },
    bmapPto(lng, lat) {
      if (lng && lat) {
        this.bMapObj.panTo(new BMap.Point(lng, lat))
      }
    },
    bmapPtoAuto(val) {
      for (let iteam of this.deviceList) {
        if (iteam.area_id === val) {
          this.bmapPto(iteam.lng, iteam.lat)
          return false
        }
      }
    },
    creatMarkInfoWindow(arr) {
      if (!arr || arr.length < 2) {
        return false
      }
      let htmlStr = settingMapHtml || ''
      let marker = new BMap.Marker(new BMap.Point(arr[0], arr[1]))
      this.bMapObj.addOverlay(marker)
      //弹窗
      let textInfoWindow = new BMap.InfoWindow('', {
        width: 400,
        height: 200,
        offset: new BMap.Size(0, 20)
      })
      textInfoWindow.setContent(htmlStr)
 
      let _this = this
 
      const opentextInfoWindow = () => {
        marker.openInfoWindow(textInfoWindow)
 
        // document.querySelector( `#${this.newMapId} #settingMapInfoBtn`).onclick=()=>{
        //   let mapName = document.querySelector( `#${this.newMapId} #settingMapName`).value;
        //   _this.settingMapInfo.name = mapName;
        //   if(mapName!==''){
        //     //创建方法
        //     this.addArea(_this.settingMapInfo);
        //   }else{
        //     this.$message({message: '区域名称还没有填写!',type: 'error'});
        //   }
        // };
      }
      setTimeout(() => {
        opentextInfoWindow()
      }, 100)
      marker.addEventListener('click', opentextInfoWindow)
      return marker
    },
    /* 设备marker渲染 */
    bmapCameraMarkers(arr) {
      let mapData = arr || []
      //更改地图中心
      if (mapData && mapData.length > 0 && mapData[0]) {
        let arrIndex = mapData.length - 1
        this.bmapPto(mapData[arrIndex].lng, mapData[arrIndex].lat)
      } else {
        return false
      }
 
      /* 摄像头全局对象 */
      let cameraMarkers = []
      /*渲染marker*/
 
      for (let i = 0; i < mapData.length; i++) {
        if (
          mapData[i].lng &&
          mapData[i].lat &&
          mapData[i].lng !== 'None' &&
          mapData[i].lat !== 'None'
        ) {
          let pt = new BMap.Point(mapData[i].lng, mapData[i].lat)
          let iconStr = '/static/img/map/bmap_camera.png'
          console.log(pt, 'arr---arr')
          /* 图标设置 */
          //不在线图标0 高亮
          let cameraIcon_active= new BMap.Icon(
            iconStr,
            new BMap.Size(36, 45),
            { anchor: new BMap.Size(18, 45) }
          )
          //在线图标1
          let cameraIcon_online = new BMap.Icon(
            iconStr,
            new BMap.Size(36, 45),
            {
              anchor: new BMap.Size(18, 45),
              imageOffset: new BMap.Size(0, -47)
            }
          )
          //图标
          let cameraIcon_unOnline = new BMap.Icon(
            iconStr,
            new BMap.Size(36, 45),
            {
              anchor: new BMap.Size(18, 45),
              imageOffset: new BMap.Size(0, -94)
            }
          )
 
          let marker = new BMap.Marker(pt, {
            icon:cameraIcon_online
          })
          this.bMapObj.addOverlay(marker)
          // Camera title
          let opts = {
            position: pt, // 指定文本标注所在的地理位置
            offset: new BMap.Size(35, -35) //设置文本偏移量
          }
          let labelHtml = `
                      <div class="pt20">
                          <h4 class="mb5"><span class="f14 pr5">【${
                            this.userInfo.sysOrgs.length &&
                            this.userInfo.sysOrgs[0]
                              ? this.userInfo.sysOrgs[0].name
                              : ''
                          }】</span><span class="text-primary">${
            mapData[i].name
          }</span> </h4>
                          <p class="mb5 pt10 pl10"><span class="f14 pr5">地址:</span>${
                            mapData[i].address
                          }</p>`
          marker.index = i
          marker.data = mapData[i]
          marker.id = mapData[i].id
 
          marker.Icon_unOnline = cameraIcon_unOnline
          marker.Icon_online = cameraIcon_online
          marker.Icon_active = cameraIcon_active
          if (findInArrJson(mapData[i].id, cameraMarkers, 'id') === -1) {
            cameraMarkers.push(marker)
          }
 
          //是否点击摄像机
 
          let _this = this
          const opentextInfoWindow = async() => {
            /* 查询学校上报情况 */
            await this.getSchoolInfoData(marker.id)
            labelHtml += `<div class="pt30 pb10 flex-center">
                          <div id="mapDailyBtn" class="mr15 ml10 cursor-pointer">
                            <div class="clearfix">
                              <img src="/static/img/map/dailyIcon.png" width="60px" class="float-left" />
                              <p class="float-left f20 pl10 mapWindowInfoP">
                                <span class="text-primary">${
                                  this.countNum && this.countNum.submited
                                    ? this.countNum.submited
                                    : 0
                                }</span>
                                <span class="text-muted small f14">(上报)</span> / 
                                <span class="text-warning">${
                                  this.countNum && this.countNum.notSubmited
                                    ? this.countNum.notSubmited
                                    : 0
                                }</span>
                                <span class="text-muted small f14">(未上报)</span>
                              </p>
                            </div>
                            <h5 class="pt10">日常上报</h5>
                          </div>
                          <div id="mapEmergencyBtn" class="mr10 ml15 cursor-pointer">
                            <div class="clearfix">
                              <img src="/static/img/map/emergencyIcon.png" width="60px" class="float-left" />
                              <p class="float-left f20 pl10 mapWindowInfoP text-danger">${
                                this.countNum && this.countNum.emergency
                                  ? this.countNum.emergency
                                  : 0
                              }</p>
                            </div>
                            <h5 class="pt10">应急上报</h5>
                          </div>
                        </div>
                      </div>`
            
            this.textInfoWindow.setContent(labelHtml)
            marker.openInfoWindow(this.textInfoWindow)
            //添加事件
            document.querySelector(
              `#${this.newMapId} #mapDailyBtn`
            ).onclick = () => {
              _this.$emit('mapDailyfn', marker)
            }
            document.querySelector(
              `#${this.newMapId} #mapEmergencyBtn`
            ).onclick = () => {
              _this.$emit('mapEmergencyfn', marker)
            }
          }
          marker.addEventListener('click', opentextInfoWindow)
 
          //回调函数
          //
        }
      }
      this.cameraMarkers = cameraMarkers
 
      console.log(cameraMarkers, 'cameraMarkers')
    },
 
    //报警列表点击事件
    checkAlarmInfo(json, index, iNum) {
      this.alarmInfoListActive = index + '-' + iNum
      this.$emit('mapAlarmInfoClick', json)
    },
    //双击关闭
    closeCheckAlarmInfo(json) {
      this.alarmInfoListActive = ''
      this.$emit('mapCloseAlarmInfoClick', json)
      this.removeActiveCameraMarkers()
    },
    //设置高亮
    setActiveCameraMarkers(arr) {
      let idArr = arr || []
      let markers = this.cameraMarkers || []
      if (idArr.length === 0) {
        return false
      }
 
      let activeDevices = []
      //遍历id查找高亮
      idArr.forEach((idItem, index) => {
        for (let iteam of this.cameraMarkers) {
          if (iteam.id == idItem.id) {
            activeDevices.push(iteam)
            //iteam.setIcon(iteam.Icon_active);
            //移动到第0个的位置
            if (index === 0) {
              this.bmapPto(iteam.data.lng, iteam.data.lat)
            }
          } else {
            iteam.setIcon(iteam.Icon_online)
          }
        }
      })
      //设置高亮图标
      for (let val of activeDevices) {
        val.setIcon(val.Icon_active)
      }
    },
    //删除高亮
    removeActiveCameraMarkers() {
      let markers = this.cameraMarkers || []
      for (let iteam of markers) {
        iteam.setIcon(iteam.Icon_online)
      }
    }
  },
  watch: {
    deviceList: {
      handler(val, oldVal) {
        this.bmapCameraMarkers(val)
      },
      deep: true
    },
    searchArr:{
      handler(val, oldVal) {
        this.setActiveCameraMarkers(val)
      },
      deep: true
    }
    // panToVal: {
    //   handler(val, oldVal) {
    //     if (val !== oldVal) {
    //       //this.bmapPtoAuto(val);
    //       this.bmapPto(val.lng, val.lat)
    //     }
    //   },
    //   deep: true
    // },
    // activeDeviceIds: {
    //   handler(val, oldVal) {
    //     if (val !== oldVal) {
    //       this.setActiveCameraMarkers(val)
    //     }
    //   },
    //   deep: true
    // },
    // removeActiveAlarmInfo: {
    //   handler(val, oldVal) {
    //     if (val !== oldVal) {
    //       this.alarmInfoListActive = ''
    //       //去除全部高亮
    //       this.removeActiveCameraMarkers()
    //     }
    //   },
    //   deep: true
    // }
  },
  created() {
    if (this.showDynamics || this.showCameras) {
      this.getDeviceData()
    }
 
    this.getMapSchoolList()
  },
  mounted() {
    /* 地图初始化 */
    this.bmapinit()
  }
}
</script>
  <style lang="scss" scoped>
@import '../../vendor/styles/colors.scss';
 
.home-chart {
  height: calc(100% - 45px);
}
.alarmInfo-list {
  position: absolute;
  right: 5px;
  top: 5px;
  background: rgba(255, 255, 255, 0.7);
  max-width: 60%;
  max-height: 200px;
  overflow: auto;
  .alarmInfo {
    background: #fff;
    color: #333;
    padding: 6px 5px;
    width: 80px;
    transition: all 0.6s;
    cursor: pointer;
    .alarmInfo-icon {
      font-size: 25px;
      padding-right: 5px;
      width: 25px;
      height: 25px;
      overflow: hidden;
      .fa-car {
        font-size: 20px;
        margin-top: 2px;
      }
    }
    .alarmInfo-count {
      font-size: 15px;
      line-height: 25px;
      height: 25px;
      font-weight: bolder;
    }
    .alarmInfo-title {
      text-align: center;
      font-size: 14px;
      padding-top: 5px;
    }
  }
  .alarmInfo.active {
    background: #2f9fb0;
    color: #fff;
  }
}
</style>