liuxiaolong
2019-05-06 2418a2d61feea858e9e63aa52fd64ddfe17e392a
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<template>
  <div>
    <div :id="newMapId" class="map"></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 { Table, TableColumn, Collapse, CollapseItem } from 'element-ui'
import { findInArrJson } from '@/components/common/util.js'
import vxg from './vxg'
export default {
  name: 'home-map',
  metaInfo: {
    title: '首页地图'
  },
  components: {
    Table,
    TableColumn,
    Collapse,
    CollapseItem,
    vxg
  },
  props: {
    // 自定义id
    newId:{
      default:'',
      type:String
    }
  },
  data() {
    return {
      /** 地图对象 */
      bMapObj: {},
      /** 摄像头全局对象 */
      cameraMarkers: [],
      /** 定义视频播放起的编号 */
      indexPlayer: 1
    }
  },
  computed: {
    newMapId() {
      return this.newId && this.newId !== '' ? this.newId : 'allMap'
    },
    newDefaultMapZoom() {
      return this.defaultMapZoom && this.defaultMapZoom !== ''
        ? this.defaultMapZoom
        : 12
    }
  },
  methods: {
    /* 地图初始化 */
    bmapinit() {
      /* eslint-disable */
      /*地图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)
      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.enableScrollWheelZoom()
      // 设置地图点击事件
      this.bMapObj.addEventListener('rightclick',this.mapClick)
 
      // this.bMapObj.addEventListener('mousedown', this.listenMouseDown)
      // this.bMapObj.addEventListener('mousemove', this.listenMouseOve)      
      // 监听鼠标松开事件
      // this.bMapObj.addEventListener('mouseup', this.listenMouseUp)
 
      //创建全局弹窗
      this.textInfoWindow = new BMap.InfoWindow('', {
        width: 480,
        height: 420,
        offset: new BMap.Size(0, -10)
      })
 
      // 创建全局右键菜单对象
      // let txtMenuItem = [
      //   {
      //     text:'放置设备',
      //     callback:function () {
      //       this.setDevice()
      //     }
      //   }
      // ]
      // let menu = new BMap.ContextMenu()
      // for(var i=0; i < txtMenuItem.length; i++){
      //   menu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,80));
      // }
      // this.bMapObj.addContextMenu(menu)
 
      /** 测试marker */
      let mapdata =[
        {
          id:'1',
          lng:'116.331398',
          lat:'39.897445',
          status: 'online',
          data:{
            lng:'116.331398',
            lat:'39.897445',
          },
          url:'rtsp://admin:a1234567@192.168.1.201:554/h264/ch1/main/av_stream'
        },
        {
          id:'2',
          lng:'116.318912',
          lat:'39.923854',
          status: 'unOnline',
          url:'rtsp://admin:a1234567@192.168.1.64:554/h264/ch1/main/av_stream',
          data:{
            lng:'116.318912',
            lat:'39.923854',
          }
        }
      ]
      this.bmapCreateMarker(mapdata)
    },
    /**城市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))
      }
    },
    //创建一个标记点信息窗口
    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渲染
    bmapCreateMarker(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
      }
      console.log(arr,'--------------ViewMap  arr')
      /** 摄像头全局对象 */
      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'
          /* 图标设置 */
          //不在线图标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)
            }
          )
          //摄像头的icon
          // let cameraIcon = new BMap.Icon('/static/img/map/camera_device_online.png',new BMap.Size(43,45))
          let marker
          if(mapData[i].status === 'online'){
            marker = new BMap.Marker(pt, {
              icon:cameraIcon_online
            })
          } else if(mapData[i].status === 'unOnline'){
            marker = new BMap.Marker(pt, {
              icon:cameraIcon_unOnline
            })
          }
          
          this.bMapObj.addOverlay(marker)
          /** 设置标注点可拖拽 测试*/
          marker.enableDragging()
 
          /** 将设备信息存储到marker中 */
          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, this.cameraMarkers, 'id') === -1) {
            this.cameraMarkers.push(marker)
          }
          console.log(this.cameraMarkers, 'this.cameraMarkers')
          //是否点击摄像机
          let _this = this
          const opentextInfoWindow = async() => {
            /* 查询设备情况 */
            // await this.getSchoolInfoData(marker.id)
            console.log('进入打开弹窗')
            let labelHtml = `<div class="pb10">
              <div style="height:70%;width:100%">
                <div id="dynamicallyPlayers"></div>
              </div>
              <div style="height:30%;" class="">
                <div class="flex-row-between">
                  <div class="flex-row-left">
                    <span>设备类型:</span>
                  </div>
                  <div id="toDetails" class="flex-row-right">
                    <a href="javascript:console.log()">详情</a>
                  </div>
                </div>
                
                <div class="flex-row-left">
                  <span>设备编码:</span>
                </div>
                <div class="flex-row-left">
                  <span>设备位置:</span>
                </div>
              </div>  
            </div>`
            
            this.textInfoWindow.setContent(labelHtml)
            marker.openInfoWindow(this.textInfoWindow)
            //添加事件
            document.querySelector(
              `#${this.newMapId} #toDetails`
            ).onclick = () => {
              _this.$emit('toDetails', marker)
            }
            // document.querySelector(
            //   `#${this.newMapId} #mapEmergencyBtn`
            // ).onclick = () => {
            //   _this.$emit('mapEmergencyfn', marker)
            // }
 
            /** 渲染弹框中的视频插件*/
            console.log('能否走到这一步')
            this.indexPlayer++
            var playerId = 'vxg_media_player1'+this.indexPlayer
            var div = document.createElement('div')
            div.setAttribute("id", playerId)
            div.setAttribute("class", "vxgplayer")
            var runtimePlayers = document.getElementById('dynamicallyPlayers')
            runtimePlayers.appendChild(div)
            console.log(vxgplayer,'vxgplayer')
            vxgplayer(playerId, {
                    url: '',
                    nmf_path: 'media_player.nmf',
                    nmf_src: '/static/vxgplayer-1.8.31/pnacl/Release/media_player.nmf',
                    latency: 300000,
                    width: "460",
                    height: "320",
                    aspect_ratio_mode: 1,
                    autohide: 3,
                    controls: true,
                    connection_timeout: 5000,
                    connection_udp: 0,
                    custom_digital_zoom: false
            }).ready(function(){
                console.log(' =>ready player '+playerId)
                vxgplayer(playerId).src(mapData[i].url)
                vxgplayer(playerId).play()
                console.log(' <=ready player '+playerId)
            })
          }
          marker.addEventListener('click', opentextInfoWindow)
 
        }
      }
    },
    // 接收父组件传递过来的数据,用以渲染地图组件
    reciveParentData(arr){
      console.log(arr,'地图组件中接收父组件传递过来的数据')
      console.log(this.cameraMarkers,'地图中的所有标记点数据')
      if(arr&&arr.length!==0){
        /**迭代渲染每一个标记点 */
        arr.forEach((item,index)=>{
          // 获取设备位置信息
          let point = new BMap.Point(item.lng,item.lat)
          // 创建标记点marker
          let marker 
          /* 图标设置 */
          let iconStr = '/static/img/map/bmap_camera.png'
          //不在线图标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)
            }
          )
          // 根据设备状态选择标签
          if(item.status === 'online'){
            marker = new BMap.Marker(point,{
              icon:cameraIcon_online
            })
          } else if (item.status === 'unOnline'){
            marker = new BMap.Marker(point,{
              icon:cameraIcon_unOnline
            })
          } else {
            marker = new BMap.Marker(point,{
              icon:cameraIcon_active
            })
          }
          /** 将设备信息存在marker中 */
          // marker.index = index
          // marker.data = item
          // marker.id = item.id
 
          /** 查找每一个设备是否在全局设备列表中,然后进行相关操作 */
          this.cameraMarkers.forEach((x,index)=>{
            /** 判断设备id是否相等 */
            if(x.id ===item.id ){
              /** 如果id相等,对这个设备在地图上的点进行高亮或跳动 */
              x.setIcon(x.Icon_active)
            }
          })
 
          // 将标记点渲染到地图上去
          this.bMapObj.addOverlay(marker)
          // 设置标记点跳动
          marker.setAnimation(BMAP_ANIMATION_BOUNCE)
          // 将标记点信息装进全局变量中
          // this.cameraMarkers.push(marker)
 
          //是否点击摄像机
          let _this = this
          const opentextInfoWindow = async() => {
            /* 查询设备情况 */
            // await this.getSchoolInfoData(marker.id)
            console.log('进入打开弹窗')
            let labelHtml = `<div class="pb10">
              <div style="height:70%;width:100%">
                <div id="dynamicallyPlayers"></div>
              </div>
              <div style="height:30%;" class="">
                <div class="flex-row-between">
                  <div class="flex-row-left">
                    <span>设备类型:</span>
                  </div>
                  <div id="toDetails" class="flex-row-right">
                    <a href="javascript:console.log()">详情</a>
                  </div>
                </div>
                
                <div class="flex-row-left">
                  <span>设备编码:</span>
                </div>
                <div class="flex-row-left">
                  <span>设备位置:</span>
                </div>
              </div>  
            </div>`
            
            this.textInfoWindow.setContent(labelHtml)
            marker.openInfoWindow(this.textInfoWindow)
            //添加事件
            document.querySelector(
              `#${this.newMapId} #toDetails`
            ).onclick = () => {
              _this.$emit('toDetails', marker)
            }
 
             /** 渲染弹框中的视频插件*/
            console.log('能否走到这一步')
            this.indexPlayer++
            var playerId = 'vxg_media_player1'+this.indexPlayer
            var div = document.createElement('div')
            div.setAttribute("id", playerId)
            div.setAttribute("class", "vxgplayer")
            var runtimePlayers = document.getElementById('dynamicallyPlayers')
            runtimePlayers.appendChild(div)
            console.log(vxgplayer,'vxgplayer')
            vxgplayer(playerId, {
                    url: '',
                    nmf_path: 'media_player.nmf',
                    nmf_src: '/static/vxgplayer-1.8.31/pnacl/Release/media_player.nmf',
                    latency: 300000,
                    width: "460",
                    height: "320",
                    aspect_ratio_mode: 1,
                    autohide: 3,
                    controls: true,
                    connection_timeout: 5000,
                    connection_udp: 0,
                    custom_digital_zoom: false
            }).ready(function(){
                console.log(' =>ready player '+playerId)
                vxgplayer(playerId).src(item.url)
                vxgplayer(playerId).play()
                console.log(' <=ready player '+playerId)
            })
          }
          marker.addEventListener('click', opentextInfoWindow)
        })
      }
    },
     //设置高亮
    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 {
            // console.log(iteam,'iteam ')
            if(iteam.data.status === 'online'){
              iteam.setIcon(iteam.Icon_online)
              iteam.setAnimation(null)
            } else if(iteam.data.status === 'unOnline'){
              iteam.setIcon(iteam.Icon_unOnline)
              iteam.setAnimation(null)
            }
          }
        }
      })
      //设置高亮图标
      for (let val of activeDevices) {
        val.setIcon(val.Icon_active)
        // 设置标记点跳动
        val.setAnimation(BMAP_ANIMATION_BOUNCE)
      }
    },
    //删除高亮
    removeActiveCameraMarkers() {
      let markers = this.cameraMarkers || []
      for (let iteam of markers) {
        if(iteam.status === 'online'){
          iteam.setIcon(iteam.Icon_online)
        } else if(iteam.status === 'unOnline'){
          iteam.setIcon(iteam.Icon_unOnline)
        }
        iteam.setAnimation(null)
      }
    },
    // 放置设备
    setDevice(e){
      console.log(this.unUseDevice, '放置设备')
      if(this.unUseDevice.length!==0){
        // 说明还有未分配的设备
 
      }
    },
    // 地图点击事件
    mapClick(e){
      let point = new BMap.Point(e.point.lng,e.point.lat)
      console.log(point,'point')
    },
    // 地图中监听鼠标松开事件,获取松开的经纬度
    listenMouseUp(e){
      let point = new BMap.Point(e.point.lng,e.point.lat)
      console.log(point,'鼠标在这里松开')
      this.$emit('getPoint',point)
    },
    listenMouseOve(e){
      let point = new BMap.Point(e.point.lng,e.point.lat)
      // console.log(point,'鼠标在这里移动')
    },
    listenMouseDown(e){
      let point = new BMap.Point(e.point.lng,e.point.lat)
      console.log(point,'鼠标在这里按下鼠标')
    }
  },
  created() {},
  mounted() {
    /* 地图初始化 */
    setTimeout(() => {
      this.bmapinit()
    }, 100)
  },
  watch: {}
}
</script>
<style lang="scss">
.map {
  height: 700px;
  width: 100%;
}
</style>