fence.js 15.4 KB
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
import EventBus from '../event-bus'
import mapService from './mapService.js'
import MAPGEO from './mapgeo'

// 电子围栏
const fence = {
  polygons: [],
  circle: null,
  isEditMode: true,
  bindEvent() {
    EventBus.$on('uiMap.fence.drawDistrict', (data) => {
      this.drawDistrict(data)
    })

    EventBus.$on('uiMap.fence.createFenceCircle', (data) => {
      if ('isEdit' in data && !data.isEdit) {
        this.isEditMode = false
      } else {
        this.isEditMode = true
      }
      this.createFenceCircle(data)
    })

    EventBus.$on('uiMap.fence.updateFenceRadius', (data) => {
      this.circle.setRadius(data * 1)
      mapService.setFitView()
    })

    EventBus.$on('uiMap.fence.createFenceMarker', (data) => {
      this.createFenceMarker(data)
    })

    EventBus.$on('uiMap.fence.drawPolygon', (data) => {
      console.log(data, Array.isArray(data))
      if (data) {
        if (Array.isArray(data)) {
          this.polygonPath = data
          this.isEditMode = true
        } else if ('isEdit' in data && !data.isEdit) {
          this.polygonPath = data.path
          this.isEditMode = false
        }
      }
      this.drawPolygon()
      console.log(this.polygonPath, '======path')
    })

    EventBus.$on('uiMap.fence.destroy', (data) => {
      this.polyEditor && this.polyEditor.close()
      this.mouseTool && this.mouseTool.close()
    })
  },
  destroy() {
    const mapapi = mapService.mapapi
    if (mapapi) {
      mapapi.invoke('clearOverlays')
      mapapi.invoke('addOverlay', this.marker)
      // 清除多边形
      if (this.drawingManager && this.drawingManager.getOverlays().length) {
        const overlays = this.drawingManager.getOverlays()[0]
        mapapi.invoke('removeOverlay', overlays)
      }
      if (document.getElementById('cancelOperate')) {
        document.getElementById('cancelOperate').click()
      }

      this.dist && mapapi.invoke('removeDistrictLayer', this.dist)
    }
    // const MAP = mapService.map
    // this.polygons && MAP.remove(this.polygons)
    // this.circle && MAP.remove(this.circle)
    // this.circleEditor && this.circleEditor.close() // MAP.remove(this.circleEditor);
    // this.polygons = []
    // this.polyEditor && this.polyEditor.close()
    // this.mouseTool && this.mouseTool.close()
    // this.polygon && MAP.remove(this.polygon)
    // this.clearTimeoutIds()
  },
  drawDistrict(data) {
    const mapapi = mapService.mapapi
    this.destroy()
    if (!data || data.length <= 0) {
      return false
    }
    if (this.dist) mapapi.invoke('removeDistrictLayer', this.dist)
    this.dist = mapapi.invoke('DistrictLayer', {
      name: `(${data})`,
      fillOpacity: 0.7,
      fillColor: '#CCF3FF',
      strokeColor: '#CC66CC',
      viewport: true
    })
    mapapi.invoke('addDistrictLayer', this.dist)
    console.log(this.dist, 'district')
    // var opts = {
    //   subdistrict: 1, // 返回下一级行政区
    //   extensions: 'all',
    //   showbiz: false // 最后一级返回街道信息
    // }
    // data == '中国' ? delete opts.extensions : delete opts.showbiz
    // const district = new AMap.DistrictSearch(opts)
    // district.setLevel('district')
    // EventBus.$emit('uiMap.fence.loading', true)
    // // 行政区查询
    // district.search(data.toString(), function(status, result) {
    //   const dst = result.districtList[0]
    //   var bounds = dst.boundaries
    //   const lng = dst.center.lng
    //   const lat = dst.center.lat

    //   if (bounds) {
    //     for (var i = 0, l = bounds.length; i < l; i++) {
    //       // 生成行政区划polygon
    //       var polygon = new AMap.Polygon({
    //         map: MAP,
    //         strokeWeight: 1,
    //         path: bounds[i],
    //         fillOpacity: 0.7,
    //         fillColor: '#CCF3FF',
    //         strokeColor: '#CC66CC'
    //       })
    //       fence.polygons.push(polygon)
    //     }

    //     const center = mapService.getLngLat(lng, lat)

    //     mapService.setFitView()
    //     EventBus.$emit('uiMap.fence.loading', false)
    //   }
    // })
  },
  updateFenceCircle(data) {
    const mapapi = mapService.mapapi
    mapapi.invoke('circleDisableEditing')
    if (data.radius && this.circle) {
      this.circle.setRadius(data.radius * 1)
    }
    if (data.lng && data.lat) {
      const center = mapService.getLngLat(data.lng, this.lat)
      this.circle && this.circle.setCenter(center)
      mapapi.invoke('panTo', center, true)
    }

    mapapi.invoke('removeOverlay', this.circle)
    mapapi.invoke('addOverlay', this.circle)
    mapapi.invoke('circleEnableEditing')
  },
  createFenceCircle(data) {
    if (!this.circle) {
      const _this = this
      const { map, mapapi } = mapService
      const mapInterval = setInterval(() => {
        if (mapapi) {
          clearInterval(mapInterval)
          const lng = data.lng || _this.lng
          const lat = data.lat || _this.lat
          if (data.lng) {
            _this.lng = data.lng
          }
          if (data.lat) {
            _this.lat = data.lat
          }
          const lnglat = mapService.getLngLat(lng, lat)
          const options = {
            strokeColor: '#F33',
            strokeOpacity: 1,
            strokeWeight: 3,
            fillColor: 'ee2200',
            fillOpacity: 0.35,
            enableEditing: true
          }
          if (!_this.isEditMode) options.enableEditing = false
          _this.circle = mapapi.invoke('Circle', lnglat, data.radius * 1, options)
          mapapi.invoke('panTo', lnglat, true)
          mapapi.invoke('addOverlay', _this.circle)
          mapapi.invoke('setZoom', 14)
          _this.bindContextMenu()
          mapapi.invoke('circleAddEventListener', 'lineupdate', function(e) {
            const overlay = e.overlay
            EventBus.$emit('uiMap.fence.onFenceLngLatChange', {
              lng: e.overlay.getCenter().lng,
              lat: e.overlay.getCenter().lat
            })
            EventBus.$emit('uiMap.fence.onFenceRadiusChange', parseInt(overlay.getRadius() / 1))
          })
        }
      }, 2000)
    } else {
      this.destroy()
      this.updateFenceCircle(data)
    }
    // console.log(data, 'createFenceCircle')
    // const MAP = mapService.map
    // const mapapi = mapService.mapapi
    // // this.destroy()
    // this.circle = mapapi.invoke('Circle', mapService.getLngLat(data.lng, data.lat), data.radius * 1, {
    //   strokeColor: '#F33',
    //   fillColor: 'ee2200',
    //   strokeWeight: 3,
    //   strokeOpacity: 1,
    //   fillOpacity: 0.35
    // })
    // mapapi.invoke('addOverlay', this.circle)
    // this.circle = new AMap.Circle({
    //   map: MAP,
    //   center: mapService.getLngLat(data.lng, data.lat),
    //   radius: data.radius * 1,
    //   strokeColor: '#F33',
    //   strokeOpacity: 1,
    //   strokeWeight: 3,
    //   fillColor: 'ee2200',
    //   fillOpacity: 0.35,
    //   zIndex: 0
    // })

    // this.circleEditor = new AMap.CircleEditor(MAP, this.circle)

    // AMap.event.addListener(this.circleEditor, 'adjust', (e) => {
    //   EventBus.$emit('uiMap.fence.onFenceRadiusChange', e.radius / 1)
    //   /* this.form.radius = e.radius/1;
    //         this.$emit('onUpdateMapData',{
    //             lng: this.form.lng,
    //             lat: this.form.lat,
    //             address: this.form.transferAddr,
    //             radius: this.form.radius
    //         });*/
    //   // this.$refs.fenceMapForm.validateField('radius');
    // })

    // AMap.event.addListener(this.circleEditor, 'move', (e) => {
    //   EventBus.$emit('uiMap.fence.onFenceLngLatChange', {
    //     lng: e.lnglat.lng,
    //     lat: e.lnglat.lat
    //   })
    // })
    // if (this.isEditMode) {
    //   this.circleEditor.open()
    // } else {
    //   this.circleEditor.close()
    // }

    // mapService.setFitView()
    // MAP.setZoom(14)
  },
  createFenceMarker(data) {
    this.data = [
      {
        blng: data.lngGPS,
        blat: data.latGPS
      }
    ]
    if (data.lngBase && data.latBase) {
      this.data.push({
        blng: data.lngBase,
        blat: data.latBase
      })
    }
    const MAP = mapService.map
    MAPGEO.transferAddressByGroup(this, () => {
      data.transferAddressGPS = this.data[0].transferAddress
      if (this.data.length > 1) {
        data.transferAddressBase = this.data[1].transferAddress
      }
      const getLngLat = mapService.getLngLat(parseFloat(data.lngGPS), parseFloat(data.latGPS))
      const marker = mapService.createCarMarker(getLngLat, MAP)
      const infoWindow = mapService.bindFenceInfoWindow(marker, MAP, data)
      GBMap.invoke('addOverlay', this.marker)
    })
  },
  drawPolygon() {
    const mapapi = mapService.mapapi
    const self = this
    console.log(self.isEditMode, this.polygonPath, '==')
    this.destroy() // 清除上一次绘制
    if (this.polygonPath.length) {
      const options = {
        strokeColor: '#FF33FF',
        strokeWeight: 6,
        strokeOpacity: 0.2,
        fillOpacity: 0.4,
        fillColor: '#1791fc',
        zIndex: 50,
        enableEditing: true
      }
      if (!self.isEditMode) {
        options.enableEditing = false
      }
      this.polygonPath = this.polygonPath.map((item) => {
        return mapService.getLngLat(item[0], item[1])
      })
      self.polygon = mapapi.invoke('Polygon', this.polygonPath, options)
      const lnglat = self.polygon.getBounds().getCenter()
      mapapi.invoke('panTo', lnglat, true)
      // MAP.add(self.polygon)
      // MAP.setFitView([self.polygon])

      // self.polyEditor = new AMap.PolyEditor(MAP, self.polygon)
      // if (self.isEditMode) {
      //   self.polyEditor.open()
      // } else {
      //   self.polyEditor.close()
      // }
      mapapi.invoke('polygonAddEventListener', 'lineupdate', function(e) {
        const pathArr = self.polygon.getPath()
        self.emitPolyEvent(pathArr)
      })
      mapapi.invoke('addOverlay', self.polygon)

      // self.bindPolyEvent(self.polyEditor)
    } else if (self.isEditMode) {
      var styleOptions = {
        strokeColor: '#FF33FF', // 边线颜色
        fillColor: '#1791fc', // 填充颜色。当参数为空时,圆形没有填充颜色
        strokeWeight: 6, // 边线宽度,以像素为单位
        strokeOpacity: 0.2, // 边线透明度,取值范围0-1
        fillOpacity: 0.4 // 填充透明度,取值范围0-1
      }
      var labelOptions = {
        borderRadius: '2px',
        background: '#FF33FF',
        border: '1px solid #E1E1E1',
        color: '#703A04',
        fontSize: '12px',
        letterSpacing: '0',
        padding: '5px'
      }
      self.drawingManager = mapapi.invoke('DrawingManager', mapService.map, {
        isOpen: true, // 是否开启绘制模式
        enableDrawingTool: true,
        enableCalculate: false, // 绘制是否进行测距测面
        enableSorption: true, // 是否开启边界吸附功能
        sorptiondistance: 20, // 边界吸附距离
        circleOptions: styleOptions, // 圆的样式
        polylineOptions: styleOptions, // 线的样式
        polygonOptions: styleOptions, // 多边形的样式
        rectangleOptions: styleOptions, // 矩形的样式
        labelOptions: labelOptions // label样式
      })
      mapapi.invoke('drawingManagerAddEventListener', 'polygoncomplete', function(e) {
        const overlays = e
        const pathArr = e.getPath()
        console.log(pathArr)
        // let resultArr = pathArr.map((item) => {
        //     return item.latLng
        // })
        self.emitPolyEvent(pathArr)
      })
      mapapi.invoke('setDrawingMode', 'polygon')
      // self.mouseTool = new AMap.MouseTool(MAP)

      // self.mouseTool.polygon({
      //   strokeColor: '#FF33FF',
      //   strokeOpacity: 1,
      //   strokeWeight: 6,
      //   strokeOpacity: 0.2,
      //   fillColor: '#1791fc',
      //   fillOpacity: 0.4,
      //   // 线样式还支持 'dashed'
      //   strokeStyle: 'solid'
      //   // strokeStyle是dashed时有效
      //   // strokeDasharray: [30,10],
      // })

      // self.mouseTool.on('draw', function(event) {
      //   // event.obj 为绘制出来的覆盖物对象
      //   self.polygon = event.obj
      //   self.polyEditor = new AMap.PolyEditor(MAP, self.polygon)
      //   self.polyEditor.open()
      //   self.bindPolyEvent(self.polyEditor)
      //   self.mouseTool.close()
      //   self.emitPolyEvent()
      // })
    }
  },
  bindPolyEvent(polyEditor) {
    const self = this
    polyEditor.on('addnode', function(event) {
      console.log('触发事件:addnode')
      self.emitPolyEvent()
    })

    polyEditor.on('adjust', function(event) {
      console.log('触发事件:adjust')
      self.emitPolyEvent()
    })

    polyEditor.on('removenode', function(event) {
      console.log('触发事件:removenode')
      self.emitPolyEvent()
    })

    polyEditor.on('end', function(event) {
      console.log('触发事件: end')
      self.emitPolyEvent()
      // event.target 即为编辑后的多边形对象
    })
  },
  emitPolyEvent(pathArr) {
    const resultArr = pathArr.map((item) => {
      return [item.lng, item.lat]
    })
    this.polygonPath = resultArr
    EventBus.$emit('uiMap.fence.polygonChange', {
      polygonPath: this.polygonPath
    })
    // const pathArr = this.polygon.getPath()

    // const resultArr = pathArr.map((item) => {
    //   return [item.lng, item.lat]
    // })

    // this.polygonPath = resultArr
    // EventBus.$emit('uiMap.fence.polygonChange', {
    //   polygonPath: this.polygonPath
    // })
    // console.log(resultArr.join('|'))
  },
  clearTimeoutIds() {
    this.timeoutIds &&
      this.timeoutIds.forEach((item) => {
        item && window.clearTimeout(item)
      })
    this.timeoutIds = []
  },
  // 地图增加右键功能
  bindContextMenu() {
    const self = this
    const mapapi = mapService.mapapi
    var contextMenu = mapapi.invoke('ContextMenu')
    contextMenu.addItem(
      mapapi.invoke('MenuItem', '设置围栏中心', function() {
        self.moveCenter()
      })
    )
    mapapi.invoke('mapAddEventListener', 'rightclick', function(e) {
      mapapi.invoke('addContextMenu', contextMenu)
      self.contextMenuPositon = e.latlng
    })
  },

  // 改变圆心
  moveCenter() {
    const mapapi = mapService.mapapi
    mapapi.invoke('circleDisableEditing')
    this.circle && this.circle.setCenter(this.contextMenuPositon)
    mapapi.invoke('removeOverlay', this.circle)
    mapapi.invoke('addOverlay', this.circle)
    mapapi.invoke('circleEnableEditing')
  },

  // 更新围栏
  updateFenceInfo(data) {
    const _this = this
    // const MAP = mapService.map
    const mapapi = mapService.mapapi
    const mapInterval = setInterval(function() {
      if (mapapi) {
        clearInterval(mapInterval)
        if (_this.fenceCircle) {
          mapapi.remove(_this.fenceCircle)
        }
        _this.fenceType = data.fenceType
        _this.polyEditor && _this.polyEditor.close()
        _this.mouseTool && _this.mouseTool.close()

        // 更新圆形围栏
        if (data.fenceType == 'circle') {
          _this.fenceCircle = null
          if (data.lng) {
            _this.lng = data.lng
          }
          if (data.lat) {
            _this.lat = data.lat
          }
          if (data.radius) {
            _this.radius = Number(data.radius)
          }
          console.log(data)
          // _this.createFenceCircle(data);
          _this.updateFenceCircle(data)
          return false
        }
        // mapService.setFitView()
        // MAP.setZoom(14)
        mapapi.invoke('setZoom', 14)
      }
    }, 100)
  }
}

export default fence