gaodeMap.js 37.8 KB
Newer Older
耿迪迪's avatar
耿迪迪 committed
1
import createPop from "./createPop";
纪泽龙's avatar
纪泽龙 committed
2
import { lineColor, mapStyle } from "./mapCommon.js";
纪泽龙's avatar
纪泽龙 committed
3 4 5
import regulatorBox from "../components/PopWindow/regulatorBox.vue";
import valveWell from "../components/PopWindow/valveWell.vue";
import flowMeter from "../components/PopWindow/flowMeter.vue";
纪泽龙's avatar
纪泽龙 committed
6
import pressureGage from "../components/PopWindow/pressureGage.vue";
纪泽龙's avatar
纪泽龙 committed
7 8
import pipelineView from "../components/PopWindow/pipelineView.vue";
import lineInfoWindow from "../components/PopWindow/lineInfoWindow.vue";
9
import { delDeviceInfo, updateDeviceInfo } from "@/api/device/deviceInfo";
耿迪迪's avatar
耿迪迪 committed
10
import markerInfoWindow from "../components/PopWindow/markerInfoWindow.vue";
纪泽龙's avatar
纪泽龙 committed
11
import { getArray } from "@/utils/gassafety.js";
12
import { delPipe, updatePipe } from "@/api/device/pipe.js";
13

纪泽龙's avatar
纪泽龙 committed
14
import vue from "../main";
耿迪迪's avatar
耿迪迪 committed
15
let defaultCenter = "石家庄";
耿迪迪's avatar
耿迪迪 committed
16
export let map;
17
export const DEVICE_TYPE = {
纪泽龙's avatar
纪泽龙 committed
18 19 20 21
  PIPEPLINE: "1",
  REGEULATORBOX: "2",
  VALUEWELL: "3",
  FLOWMETER: "4",
yaqizhang's avatar
yaqizhang committed
22
  DUTYPERSON: "5",
23
  WORKORDER: "6",
24 25
  PRESSUREGAGE: "7",
  INSPECTOR: "8"
纪泽龙's avatar
纪泽龙 committed
26
};
耿迪迪's avatar
耿迪迪 committed
27
class gaodeMap {
纪泽龙's avatar
纪泽龙 committed
28 29 30 31 32 33
  // 所有线的数组
  polyLines = [];
  // 是否开启新增
  lineType = 0;
  // 创建一个新的lineObj
  newLineObj = null;
纪泽龙's avatar
纪泽龙 committed
34 35
  // onceFlag=false;
  lineFlag = false;
纪泽龙's avatar
纪泽龙 committed
36
  mouseTool = null;
纪泽龙's avatar
纪泽龙 committed
37
  myMap = null;
纪泽龙's avatar
纪泽龙 committed
38
  showInfoWindow = null;
39
  view = null;
纪泽龙's avatar
纪泽龙 committed
40 41
  // 地图移动结束后出现infowindow用的函数
  handleInfoWindowOpenFunc = null;
42 43
  // 点击左侧抽屉的时候,改变的flag
  leftListClick = false;
纪泽龙's avatar
纪泽龙 committed
44

耿迪迪's avatar
耿迪迪 committed
45
  //构造函数中设置中央点默认值
纪泽龙's avatar
纪泽龙 committed
46
  constructor(center, latlng, containerId) {
47
    let container = containerId ? containerId : "container";
48
    this.markers = [];
纪泽龙's avatar
纪泽龙 committed
49
    this.center = center ? center : defaultCenter;
50
    map = new AMap.Map(container, {
耿迪迪's avatar
耿迪迪 committed
51 52 53 54
      //mask: addMask(result.districtList[0].boundaries),
      center: [114.72995, 38.37417],
      // resizeEnable: true,
      disableSocket: true,
纪泽龙's avatar
纪泽龙 committed
55
      viewMode: "3D",
耿迪迪's avatar
耿迪迪 committed
56 57 58
      showLabel: true,
      // labelzIndex: 110,
      pitch: 8,
59
      zoom: 12,
纪泽龙's avatar
纪泽龙 committed
60
      mapStyle
纪泽龙's avatar
纪泽龙 committed
61
      // mapStyle: 'amap://styles/darkblue',
耿迪迪's avatar
耿迪迪 committed
62 63
      // mapStyle: 'amap://styles/3b679a15f448a4740ba2ff7524e1a4ae',
    });
纪泽龙's avatar
纪泽龙 committed
64
    this.myMap = map;
纪泽龙's avatar
纪泽龙 committed
65

纪泽龙's avatar
纪泽龙 committed
66
    this.myMap.on("click", e => {
纪泽龙's avatar
纪泽龙 committed
67
      // if (this.lineType !=1 || this.lineFlag) return;
68 69
      this.closeInfoWindow();
      this.polyLinesColorClear();
纪泽龙's avatar
纪泽龙 committed
70 71
      if (this.lineType != 1 || this.lineFlag) return;

纪泽龙's avatar
纪泽龙 committed
72
      // 除了这里,还要利用vue页面的window事件辅助,当组件出来的时候,就得利用window事件
纪泽龙's avatar
纪泽龙 committed
73 74 75 76 77 78 79 80 81 82 83
      if (this.newLineObj) {
        vue
          .$confirm("是否重新画管道")
          .then(() => {
            this.newLineObj.polyEditor.close();
            map.remove(this.newLineObj);
            this.newLineObj = null;
            this.lineFlag = false;
            this.createNewLine();
            console.log("map点击事件新建");
          })
84
          .catch(() => {});
纪泽龙's avatar
纪泽龙 committed
85
      }
纪泽龙's avatar
纪泽龙 committed
86
      // console.log("抬起来了");
纪泽龙's avatar
纪泽龙 committed
87
    });
纪泽龙's avatar
纪泽龙 committed
88

89 90 91
    // 地图开始平移删除infowindow
    this.myMap.on("movestart", () => {
      // console.log("地图平移");
92
      // 抽屉点击的时候让infowindow隐藏
93

94
      if (this.leftListClick) {
95
        console.log("moveStart");
96 97
        this.closeInfoWindow();
      }
98 99 100 101
    });
    // 地图开始缩放删除infowindow
    this.myMap.on("zoomstart", () => {
      this.closeInfoWindow();
102
      this.polyLinesColorClear();
103
    });
104 105
    // 鼠标在地图上移动
    this.myMap.on("mousemove", e => {
耿迪迪's avatar
耿迪迪 committed
106 107 108
      if(!this.mouseTool){
         return;
      }
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      if (this.mouseTool.overlays.polyline.length > 0) {
        const arr = this.mouseTool.overlays.polyline;
        const length = parseInt(arr[0].getLength());
        if (this.lengthMarker) {
          this.myMap.remove(this.lengthMarker);
        }
        const position = e.lnglat;
        this.lengthMarker = new AMap.Marker({
          map: map,
          position,
          content: `<div style="color:#000;font-size:14px;display:inline-block;background:#fff;white-space:nowrap;padding:2px 5px;">
          管道长度:<span>${length}米</span><div>`,
          offset: new AMap.Pixel(20, 20)
        });
      }
    });
125
    this.myMap.on("moveend", () => {
纪泽龙's avatar
纪泽龙 committed
126 127 128
      if (this.handleInfoWindowOpenFunc) {
        this.handleInfoWindowOpenFunc();
        this.handleInfoWindowOpenFunc = null;
129 130
        // 移动结束之后,leftListClick变成false
        this.leftListClick = false;
纪泽龙's avatar
纪泽龙 committed
131
      }
132 133 134
      // console.log("缩放开始")
      if (!this.onceFlag) {
        this.onceFlag = true;
纪泽龙's avatar
纪泽龙 committed
135 136 137 138 139
        if (this.view) {
          this.view.backFlag = false;
          // 让抽屉不在隐身
          this.view.drawerOpacity = true;
        }
140
      }
141 142 143 144
      // 每次地图移动结束,就让地图可以缩放
      let options = this.myMap.getStatus();
      options.scrollWheel = true;
      this.myMap.setStatus(options);
145
      console.log("飞行结束");
146 147
    });

yaqizhang's avatar
yaqizhang committed
148
    this.districtBoundaries(latlng);
耿迪迪's avatar
耿迪迪 committed
149
  }
纪泽龙's avatar
纪泽龙 committed
150 151 152
  panTo(lnglat) {
    this.myMap.panTo(lnglat);
  }
153 154 155 156 157 158 159 160 161 162 163 164 165 166

  changeMap(bool) {
    // 卫星图
    if (!bool) {
      if (this.satellite) return;
      this.satellite = new AMap.TileLayer.Satellite();
      this.myMap.addLayer(this.satellite);
    } else {
      if (this.satellite) {
        this.myMap.removeLayer(this.satellite);
        this.satellite = null;
      }
    }
  }
耿迪迪's avatar
耿迪迪 committed
167 168 169 170
  /**
   * 获取map
   * @returns {AMap.Map}
   */
纪泽龙's avatar
纪泽龙 committed
171
  get getMap() {
耿迪迪's avatar
耿迪迪 committed
172 173 174 175 176 177 178
    return this.map;
  }

  /**
   * 重新设置中央点
   * @param value
   */
纪泽龙's avatar
纪泽龙 committed
179
  set setCenter(value) {
耿迪迪's avatar
耿迪迪 committed
180 181 182 183
    this.center = value;
    this.districtBoundaries();
  }

纪泽龙's avatar
纪泽龙 committed
184
  resetMapCenter(value) {
yaqizhang's avatar
yaqizhang committed
185 186 187
    map.setCenter(value);
  }

耿迪迪's avatar
耿迪迪 committed
188 189 190
  /**
   * 设置城市边界
   */
纪泽龙's avatar
纪泽龙 committed
191

yaqizhang's avatar
yaqizhang committed
192
  districtBoundaries(latlng) {
纪泽龙's avatar
纪泽龙 committed
193 194 195 196 197 198 199
    let that = this;
    let opts = {
      subdistrict: 0,
      extensions: "all",
      level: "city"
    };
    let district = new AMap.DistrictSearch(opts);
200
    district.search(that.center, function(status, result) {
纪泽龙's avatar
纪泽龙 committed
201 202 203 204
      if (status == "complete") {
        let defaultCenter = [];
        defaultCenter.push(result.districtList[0].center.lng);
        defaultCenter.push(result.districtList[0].center.lat);
yaqizhang's avatar
yaqizhang committed
205 206 207 208 209 210
        if (latlng) {
          map.setCenter(latlng);
        } else {
          map.setCenter(defaultCenter);
        }

211
        console.log("defaultCenter", defaultCenter);
yaqizhang's avatar
yaqizhang committed
212

纪泽龙's avatar
纪泽龙 committed
213 214 215 216 217 218 219 220 221 222 223 224 225
        let bounds = result.districtList[0].boundaries;
        for (let i = 0; i < bounds.length; i += 1) {
          new AMap.Polyline({
            path: bounds[i],
            strokeColor: "#0088ff",
            strokeWeight: 5,
            strokeOpacity: 7,
            map: map
          });
        }
      }
    });
  }
耿迪迪's avatar
耿迪迪 committed
226 227 228

  /**
   * 添加Market
229
   * @param markerType
耿迪迪's avatar
耿迪迪 committed
230 231
   * @param lnglats
   */
纪泽龙's avatar
纪泽龙 committed
232
  addMarker(markerType, data, func) {
233 234
    let that = this;
    that.markerType = markerType;
235
    console.log(markerType);
纪泽龙's avatar
纪泽龙 committed
236 237
    let infoWindow = new AMap.InfoWindow({
      isCustom: true,
238
      offset: new AMap.Pixel(24, -38),
纪泽龙's avatar
纪泽龙 committed
239 240
      anchor: "left-top"
    });
241
    infoWindow.on("mouseover", function() {
242 243 244 245
      let options = map.getStatus();
      options.scrollWheel = false;
      map.setStatus(options);
    });
246
    infoWindow.on("mouseout", function() {
247 248 249 250
      let options = map.getStatus();
      options.scrollWheel = true;
      map.setStatus(options);
    });
耿迪迪's avatar
耿迪迪 committed
251
    let marker = new AMap.Marker({
纪泽龙's avatar
纪泽龙 committed
252
      position: [data.longitude, data.latitude],
耿迪迪's avatar
耿迪迪 committed
253
      map: map,
254
      offset: new AMap.Pixel(0, 5)
耿迪迪's avatar
耿迪迪 committed
255 256
    });
    this.setMarkerIcon(marker);
257 258
    if (
      DEVICE_TYPE.WORKORDER != markerType &&
259 260
      DEVICE_TYPE.INSPECTOR != markerType &&
      func != "false"
261
    ) {
耿迪迪's avatar
耿迪迪 committed
262
      marker.content = this.getMarketContent(data);
纪泽龙's avatar
纪泽龙 committed
263
      marker.on("mouseover", this.infoOpen);
264
      marker.on("click", this.infoOpen);
yaqizhang's avatar
yaqizhang committed
265
      marker.on("mouseout", infoClose);
纪泽龙's avatar
纪泽龙 committed
266
      marker.infoWindow = infoWindow;
yaqizhang's avatar
yaqizhang committed
267 268
      marker.setExtData(data);
      that.deviceType = markerType;
269
      let editWindow = that.createInfowindow("编辑");
纪泽龙's avatar
纪泽龙 committed
270 271 272 273
      editWindow.fileArr =
        data.iconUrl != "" && data.iconUrl != undefined
          ? [{ url: data.iconUrl }]
          : [];
274 275 276
      editWindow.obj = marker;
      editWindow.gaoMap = that;
      editWindow.map = map;
277
      marker.on("click", function(e) {
yaqizhang's avatar
yaqizhang committed
278
        if ("edit" == that.mapOperateType) {
279 280 281 282 283 284 285
          if(editWindow.form.longitude && editWindow.form.latitude){
            editWindow.form = e.target.getExtData();
            editWindow.form.longitude = marker.getPosition().lng;
            editWindow.form.latitude = marker.getPosition().lat;
          }else{
            editWindow.form = e.target.getExtData();
          }
yaqizhang's avatar
yaqizhang committed
286 287
          editWindow.show();
        } else if ("delete" == that.mapOperateType) {
288 289 290 291 292 293 294 295 296
          let diviceType = {
            "2": "调压箱",
            "3": "阀门井",
            "4": "流量计",
            "7": "压力表"
          };
          vue
            .$confirm(
              '是否删除名称为"' +
297 298 299 300
                e.target.getExtData().deviceName +
                '"的' +
                diviceType[markerType] +
                " ?",
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
              "提示",
              {
                type: "warning"
                // center: true
              }
            )
            .then(() => {
              // that.deleteMarker(markerType, data.deviceId);
              that.deleteMarker(markerType, data).then(res => {
                if (res.code == 200) {
                  map.remove(marker);
                  // 数组里也删了
                  const ind = that.markers.indexOf(marker);
                  if (ind >= 0) {
                    that.markers.splice(ind, 1);
316
                    console.log(that.markers);
317
                  }
318 319 320 321 322
                  const arr = that.markers.map(item => {
                    return item.getExtData();
                  });
                  that.view.deviceClassify(arr);
                  that.view.pipeList();
323 324 325 326
                }
              });
            })
            .catch(() => {
327 328 329 330 331 332 333
              vue.$message({
                type: "info",
                // center:true,
                offset: 100,
                message: "已取消删除"
              });
            });
334
        } /*else {
335
          //map.setZoomAndCenter(13, e.target.getPosition());
yaqizhang's avatar
yaqizhang committed
336 337
          let infoWindow = new AMap.InfoWindow({
            isCustom: true,
338
            offset: new AMap.Pixel(24, -38),
yaqizhang's avatar
yaqizhang committed
339 340 341 342
            anchor: "left-top"
          });
          infoWindow.setContent(e.target.content);
          infoWindow.open(map, e.target.getPosition());
343
        }*/
yaqizhang's avatar
yaqizhang committed
344
      });
345
      marker.on("dragend", function(e) {
yaqizhang's avatar
yaqizhang committed
346 347 348
        editWindow.form.longitude = e.lnglat.lng;
        editWindow.form.latitude = e.lnglat.lat;
      });
349
    } else {
350
      marker.on("mouseover", function() {
yaqizhang's avatar
yaqizhang committed
351
        marker.setLabel({
352 353 354
          offset: new AMap.Pixel(0, -10), //设置文本标注偏移量
          content: "<div class='info'>" + data.deviceName + "</div>", //设置文本标注内容
          direction: "top" //设置文本标注方位
yaqizhang's avatar
yaqizhang committed
355 356
        });
      });
357
      marker.on("mouseout", function() {
yaqizhang's avatar
yaqizhang committed
358 359
        marker.setLabel({
          content: null
360
        });
yaqizhang's avatar
yaqizhang committed
361
      });
yaqizhang's avatar
yaqizhang committed
362
    }
耿迪迪's avatar
耿迪迪 committed
363

364 365 366 367 368 369 370
    if (this.changePolineListNum === null) {
      // this.polyLines.push(polyline);
      this.markers.push(marker);
    } else {
      this.markers.splice(this.changePolineListNum, 0, marker);
      this.changePolineListNum = null;
    }
371
    map.setZoom("12");
耿迪迪's avatar
耿迪迪 committed
372
    //map.setFitView();
耿迪迪's avatar
耿迪迪 committed
373 374

    function infoClose(e) {
375 376 377 378
      let options = map.getStatus();
      options.scrollWheel = true;
      map.setStatus(options);
      //infoWindow.close(map, e.target.getPosition());
耿迪迪's avatar
耿迪迪 committed
379
    }
纪泽龙's avatar
纪泽龙 committed
380
    if (func) {
纪泽龙's avatar
纪泽龙 committed
381
      if (func != "false") {
382 383
        func();
      }
耿迪迪's avatar
耿迪迪 committed
384
    }
385
  }
纪泽龙's avatar
纪泽龙 committed
386 387
  // 其他设备
  infoOpen = e => {
388 389 390 391 392 393
    // 如果是无状态并且鼠标mouseover
    if (this.view.targetNum == 0 && e.type == "mouseover") {
      return;
    }
    // 如果是新建状态 不出弹框
    if (this.view.targetNum == 1) return;
394
    console.log(e.target);
纪泽龙's avatar
纪泽龙 committed
395 396 397 398 399 400 401
    let options = map.getStatus();
    options.scrollWheel = false;
    map.setStatus(options);
    e.target.infoWindow.setContent(e.target.content);
    e.target.infoWindow.open(map, e.target.getPosition());
    this.markerInfoWindow = e.target.infoWindow;
  };
耿迪迪's avatar
耿迪迪 committed
402 403 404
  /**
   * marker添加拖拽事件
   */
纪泽龙's avatar
纪泽龙 committed
405 406
  addMarkerDragg() {
    for (var i = 0; i < this.markers.length; i++) {
耿迪迪's avatar
耿迪迪 committed
407 408 409 410 411 412 413
      this.markers[i].setDraggable(true);
    }
  }

  /**
   * marker移除拖拽事件
   */
纪泽龙's avatar
纪泽龙 committed
414 415
  removeMarkerDragg() {
    for (var i = 0; i < this.markers.length; i++) {
耿迪迪's avatar
耿迪迪 committed
416 417 418 419
      this.markers[i].setDraggable(false);
    }
  }

纪泽龙's avatar
纪泽龙 committed
420 421 422 423 424 425
  /**
   * 添加marker信息
   * @param data
   * @returns {string}
   */
  getMarketContent(data) {
耿迪迪's avatar
耿迪迪 committed
426 427
    switch (this.markerType) {
      case DEVICE_TYPE.REGEULATORBOX: {
纪泽龙's avatar
纪泽龙 committed
428
        const dom = createPop(markerInfoWindow, {
耿迪迪's avatar
耿迪迪 committed
429
          title: "调压箱",
耿迪迪's avatar
耿迪迪 committed
430
          data: data,
纪泽龙's avatar
纪泽龙 committed
431
          map: map
耿迪迪's avatar
耿迪迪 committed
432 433 434
        });
        const html = dom.$el;
        dom.remove();
耿迪迪's avatar
耿迪迪 committed
435 436 437
        return html;
      }
      case DEVICE_TYPE.VALUEWELL: {
纪泽龙's avatar
纪泽龙 committed
438
        const dom = createPop(markerInfoWindow, {
耿迪迪's avatar
耿迪迪 committed
439
          title: "阀门井",
耿迪迪's avatar
耿迪迪 committed
440
          data: data,
纪泽龙's avatar
纪泽龙 committed
441
          map: map
耿迪迪's avatar
耿迪迪 committed
442 443 444
        });
        const html = dom.$el;
        dom.remove();
耿迪迪's avatar
耿迪迪 committed
445 446 447
        return html;
      }
      case DEVICE_TYPE.FLOWMETER: {
纪泽龙's avatar
纪泽龙 committed
448
        const dom = createPop(markerInfoWindow, {
耿迪迪's avatar
耿迪迪 committed
449
          title: "流量计",
耿迪迪's avatar
耿迪迪 committed
450
          data: data,
纪泽龙's avatar
纪泽龙 committed
451
          map: map
耿迪迪's avatar
耿迪迪 committed
452 453 454
        });
        const html = dom.$el;
        dom.remove();
耿迪迪's avatar
耿迪迪 committed
455 456
        return html;
      }
457
      case DEVICE_TYPE.PRESSUREGAGE: {
纪泽龙's avatar
纪泽龙 committed
458
        const dom = createPop(markerInfoWindow, {
459 460
          title: "压力表",
          data: data,
纪泽龙's avatar
纪泽龙 committed
461
          map: map
462 463 464 465 466
        });
        const html = dom.$el;
        dom.remove();
        return html;
      }
耿迪迪's avatar
耿迪迪 committed
467 468 469
    }
  }

耿迪迪's avatar
耿迪迪 committed
470 471 472 473 474
  /**
   * 删除marker
   * @param delType
   * @param deviceId
   */
475
  deleteMarker(delType, deviceData) {
纪泽龙's avatar
纪泽龙 committed
476 477 478 479 480 481
    if (
      DEVICE_TYPE.REGEULATORBOX == delType ||
      DEVICE_TYPE.VALUEWELL == delType ||
      DEVICE_TYPE.FLOWMETER == delType ||
      DEVICE_TYPE.PRESSUREGAGE == delType
    ) {
482 483 484 485
      // delDeviceInfo(deviceId);
      const upDataObj = { ...deviceData };
      upDataObj.isDel = 1;
      return updateDeviceInfo(upDataObj);
耿迪迪's avatar
耿迪迪 committed
486 487 488
    }
  }

耿迪迪's avatar
耿迪迪 committed
489 490 491 492
  /**
   * 设置设备图标
   * @param marker
   */
纪泽龙's avatar
纪泽龙 committed
493
  setMarkerIcon(marker) {
纪泽龙's avatar
纪泽龙 committed
494 495
    switch (this.markerType) {
      case DEVICE_TYPE.REGEULATORBOX: {
耿迪迪's avatar
耿迪迪 committed
496 497
        let icon = new AMap.Icon({
          //size: new AMap.Size(51, 23),
纪泽龙's avatar
纪泽龙 committed
498
          image: require("../assets/images/tiaoyaxiang.svg")
耿迪迪's avatar
耿迪迪 committed
499 500 501
        });
        marker.setIcon(icon);
        break;
502
      }
纪泽龙's avatar
纪泽龙 committed
503
      case DEVICE_TYPE.VALUEWELL: {
耿迪迪's avatar
耿迪迪 committed
504 505
        let icon = new AMap.Icon({
          //size: new AMap.Size(51, 23),
纪泽龙's avatar
纪泽龙 committed
506
          image: require("../assets/images/famenjing.svg")
耿迪迪's avatar
耿迪迪 committed
507 508 509
        });
        marker.setIcon(icon);
        break;
510
      }
纪泽龙's avatar
纪泽龙 committed
511
      case DEVICE_TYPE.FLOWMETER: {
耿迪迪's avatar
耿迪迪 committed
512 513
        let icon = new AMap.Icon({
          //size: new AMap.Size(51, 23),
纪泽龙's avatar
纪泽龙 committed
514
          image: require("../assets/images/liuliangji.svg")
耿迪迪's avatar
耿迪迪 committed
515 516 517
        });
        marker.setIcon(icon);
        break;
518
      }
纪泽龙's avatar
纪泽龙 committed
519
      case DEVICE_TYPE.DUTYPERSON: {
耿迪迪's avatar
耿迪迪 committed
520 521
        let icon = new AMap.Icon({
          //size: new AMap.Size(51, 23),
纪泽龙's avatar
纪泽龙 committed
522
          image: require("../assets/images/zhibanrenyuan.svg")
耿迪迪's avatar
耿迪迪 committed
523 524 525
        });
        marker.setIcon(icon);
        break;
526
      }
yaqizhang's avatar
yaqizhang committed
527 528 529
      case DEVICE_TYPE.WORKORDER: {
        let icon = new AMap.Icon({
          //size: new AMap.Size(51, 23),
纪泽龙's avatar
纪泽龙 committed
530
          image: require("../assets/images/zhibanrenyuan.svg")
yaqizhang's avatar
yaqizhang committed
531 532 533 534
        });
        marker.setIcon(icon);
        break;
      }
535 536 537
      case DEVICE_TYPE.PRESSUREGAGE: {
        let icon = new AMap.Icon({
          //size: new AMap.Size(51, 23),
纪泽龙's avatar
纪泽龙 committed
538
          image: require("../assets/images/yalibiao.svg")
539 540 541 542
        });
        marker.setIcon(icon);
        break;
      }
543 544 545
      case DEVICE_TYPE.INSPECTOR: {
        let icon = new AMap.Icon({
          //size: new AMap.Size(51, 23),
纪泽龙's avatar
纪泽龙 committed
546
          image: require("../assets/images/zhibanrenyuan.svg")
547 548 549 550
        });
        marker.setIcon(icon);
        break;
      }
耿迪迪's avatar
耿迪迪 committed
551
    }
耿迪迪's avatar
耿迪迪 committed
552 553 554 555 556 557
  }

  /**
   * 添加折线
   * @param path
   */
纪泽龙's avatar
纪泽龙 committed
558
  addPolyline(arr, func) {
纪泽龙's avatar
纪泽龙 committed
559
    // this` polyLines = [];
纪泽龙's avatar
纪泽龙 committed
560 561 562
    console.log("包装的数组", arr);
    for (let i = 0; i < arr.length; i++) {
      const item = arr[i];
563
      let { coordinates, pipePressure } = item;
纪泽龙's avatar
纪泽龙 committed
564 565 566 567 568
      // console.log("coordinates",coordinates)
      // let path = coordinates ? getArray(coordinates) :[];
      // 字符串转二维数组

      let path = coordinates ? eval(coordinates) : [];
耿迪迪's avatar
耿迪迪 committed
569
      let polyline = new AMap.Polyline({
纪泽龙's avatar
纪泽龙 committed
570
        path,
571
        strokeColor: lineColor[pipePressure],
纪泽龙's avatar
1  
纪泽龙 committed
572
        strokeWeight: 4,
耿迪迪's avatar
耿迪迪 committed
573 574
        strokeOpacity: 0.9,
        zIndex: 50,
575 576
        cursor: "pointer",
        bubble: false,
纪泽龙's avatar
纪泽龙 committed
577 578 579 580
        geodesic: true,
        extData: {
          type: "line",
          //当前line状态 0:正常状态 1:正在编辑状态
纪泽龙's avatar
纪泽龙 committed
581 582
          isState: 0,
          lineData: item
纪泽龙's avatar
纪泽龙 committed
583 584
        }
      });
585 586
      // 如果是修改后包装,有记录的index 就在原位置插入,如果没有,就直接在最后插入
      if (this.changePolineListNum === null) {
纪泽龙's avatar
纪泽龙 committed
587
        this.polyLines.push(polyline);
588 589 590
      } else {
        this.polyLines.splice(this.changePolineListNum, 0, polyline);
        this.changePolineListNum = null;
纪泽龙's avatar
纪泽龙 committed
591
      }
纪泽龙's avatar
纪泽龙 committed
592
      // 信息窗体
纪泽龙's avatar
纪泽龙 committed
593
      const dom = createPop(lineInfoWindow, {
纪泽龙's avatar
纪泽龙 committed
594
        obj: { a: 123, editorPage: true, ...item, polyline }
纪泽龙's avatar
纪泽龙 committed
595
      });
纪泽龙's avatar
纪泽龙 committed
596

纪泽龙's avatar
纪泽龙 committed
597
      console.log("dom", dom.$el);
纪泽龙's avatar
纪泽龙 committed
598 599 600 601 602 603 604 605 606 607
      dom.$el.addEventListener("mouseover", () => {
        let options = this.myMap.getStatus();
        options.scrollWheel = false;
        this.myMap.setStatus(options);
      });
      dom.$el.addEventListener("mouseout", () => {
        let options = this.myMap.getStatus();
        options.scrollWheel = true;
        this.myMap.setStatus(options);
      });
纪泽龙's avatar
纪泽龙 committed
608
      let infoWindow = new AMap.InfoWindow({
纪泽龙's avatar
纪泽龙 committed
609
        isCustom: true,
610
        autoMove: true,
纪泽龙's avatar
纪泽龙 committed
611
        content: dom.$el,
纪泽龙's avatar
纪泽龙 committed
612
        //信息船体偏移量
纪泽龙's avatar
纪泽龙 committed
613
        offset: new AMap.Pixel(0, 0),
纪泽龙's avatar
纪泽龙 committed
614 615 616
        anchor: "left-top"
      });

纪泽龙's avatar
纪泽龙 committed
617
      this.newLineAddEvent(polyline);
纪泽龙's avatar
纪泽龙 committed
618
      polyline.infoWindow = infoWindow;
纪泽龙's avatar
纪泽龙 committed
619
      //添加事件
纪泽龙's avatar
纪泽龙 committed
620
      polyline.on("mouseover", this.polylineMouseOver);
621
      polyline.on("click", this.polylineMouseOver);
622 623 624
      polyline.on("mousedown", e => {
        this.polyLinesColorClear(polyline);
      });
纪泽龙's avatar
纪泽龙 committed
625
      polyline.on("mouseout", e => {
626 627 628
        // polyline.setOptions({ strokeColor: "#2EE7E7" });
        polyline.setOptions({ strokeColor: lineColor[pipePressure] });

纪泽龙's avatar
纪泽龙 committed
629
        // infoWindow.close();
纪泽龙's avatar
纪泽龙 committed
630
      });
纪泽龙's avatar
纪泽龙 committed
631
      // 计算info的位置
纪泽龙's avatar
纪泽龙 committed
632
      // function infoPosition() {}
纪泽龙's avatar
纪泽龙 committed
633 634
    }
    map.add(this.polyLines);
纪泽龙's avatar
纪泽龙 committed
635 636 637
    if (func) {
      func();
    }
纪泽龙's avatar
纪泽龙 committed
638 639 640
    // 缩放地图到合适的视野级别
    // map.setFitView();
  }
纪泽龙's avatar
纪泽龙 committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
  // 根据压力过滤管道
  pipePressureFilter(pipePressureArr, func) {
    this.polyLines.forEach(item => {
      const ind =
        pipePressureArr.indexOf(+item.getExtData().lineData.pipePressure) >= 0;
      // 根据压力过滤出来的显示,过滤掉的管道隐藏
      if (ind) {
        item.show();
      } else {
        item.hide();
      }
    });
    // 改变数据类型
    const newArr = this.polyLines.map(item => {
      return item.getExtData().lineData;
    });
    if (func) {
      func(newArr);
    }
  }
纪泽龙's avatar
纪泽龙 committed
661 662
  // 鼠标移入管道
  polylineMouseOver = e => {
663
    console.log(e.type);
纪泽龙's avatar
纪泽龙 committed
664
    let polyline = e.target;
665 666 667 668 669
    // 如果是无状态或者是新建并且是鼠标移入
    if (this.view.targetNum == 0 && e.type == "mouseover") {
      polyline.setOptions({ strokeColor: "#F7FE38" });
      return;
    }
670

671 672
    // 当选择的是新建的时候,线是点不了的
    if (this.lineType == 1 || this.view.targetNum == 1) return;
673 674 675 676
    if (
      (this.view.targetNum == 2 || this.view.targetNum == 3) &&
      e.type == "mouseover"
    ) {
677 678
      this.polyLinesColorClear(polyline);
    }
纪泽龙's avatar
纪泽龙 committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    // 上方导航的高
    let X = 20,
      Y = -20;
    if (e.originEvent) {
      const topBar = 81;
      // 坐标导航的宽
      const leftBar = 100;
      // 屏幕可视区的宽高
      const {
        clientWidth: windowClientWidth,
        clientHeight: windowClientHeight
      } = document.body;
      // 弹出的信息窗口的宽高

      const { offsetWidth: infoWindowWidth, offsetHeight: infoWindowHeight } = {
        offsetWidth: 406,
        offsetHeight: 316
      };
      // 鼠标碰到线后的位置
      const { clientX: mouseClientX, clientY: mouseClientY } = e.originEvent;
      // 鼠标到左边界的距离
      const offsetLeftX = mouseClientX - 100;
      // 鼠标到右边界的距离
      const offsetRightX = windowClientWidth - mouseClientX;
      const offsetTopY = mouseClientY - 81;
      const offsetBottomY = windowClientHeight - mouseClientY;

      const offsetY = mouseClientY - 80 - infoWindowHeight;

      if (offsetLeftX <= infoWindowWidth) {
        console.log("靠左了");
        X = 20;
      } else if (offsetRightX <= infoWindowWidth) {
        console.log("靠右了");
        X = -infoWindowWidth - 20;
      }
      if (offsetTopY <= infoWindowHeight) {
        console.log("靠上了");
        Y = 20;
      } else if (offsetBottomY <= infoWindowHeight + 81) {
        console.log("靠下了");
        Y = -infoWindowHeight - 20;
      }
722 723 724 725 726 727 728
      X = 23;
      Y = -20;
      // polyline.setOptions({ strokeColor: "#FF5A67" });
      // 浮动的时候为新建时,不变色
      if (this.view.targetNum != 1) {
        polyline.setOptions({ strokeColor: "#F7FE38" });
      }
纪泽龙's avatar
纪泽龙 committed
729 730 731
    }

    polyline.infoWindow.setOffset(new AMap.Pixel(X, Y));
732

733 734
    // 如果是戍边浮动出现infowindow 就让窗口在固定的位置出现,这样就不显得乱了
    if (e.type == "mouseover") {
735 736 737
      const coordinates = polyline.getPath().map(item => {
        return [item.lng, item.lat];
      });
738
      // const { coordinates } = polyline.getExtData().lineData;
739
      // 计算出前两个点的中点
740 741
      const arr1 = coordinates[0];
      const arr2 = coordinates[1];
742 743 744 745
      const a = this.centerNum(arr1[0], arr2[0]);
      const b = this.centerNum(arr1[1], arr2[1]);
      const a2 = this.centerNum(a, arr1[0]);
      const b2 = this.centerNum(b, arr1[1]);
746 747
      // polyline.infoWindow.open(map, [a2, b2]);
      polyline.infoWindow.open(map, e.lnglat);
748
    } else {
749 750
      console.log("windowOpen");
      // 变成异步,最后执行
751
      polyline.infoWindow.open(map, e.lnglat);
752 753 754 755 756 757

      // setTimeout(() => {
      //   console.log(polyline.infoWindow.getIsOpen())
      //   polyline.infoWindow.open(map, e.lnglat);
      // }, 0);
      // console.log(polyline.infoWindow.)
758
    }
纪泽龙's avatar
纪泽龙 committed
759 760 761 762 763 764
    // polyline.infoWindow=infoWindow;
    this.showInfoWindow = polyline.infoWindow;

    // const
  };

765
  polyLinesColorClear(polylines) {
766
    this.polyLines.forEach(item => {
767
      if (item != polylines) {
768 769 770
        const { pipePressure } = item.getExtData().lineData;
        // item.setOptions({ strokeColor: "#2EE7E7" });
        item.setOptions({ strokeColor: lineColor[pipePressure] });
771
      }
772 773
    });
  }
774 775
  centerNum(num1, num2) {
    return (Number(num1) + Number(num2)) / 2;
776
  }
纪泽龙's avatar
纪泽龙 committed
777 778
  // 创建一条新的线
  createNewLine() {
779
    map.remove(this.markerOverlays);
780
    console.log("创造一条线了啊");
781 782 783 784 785 786
    // 线测距
    map.plugin(["AMap.MouseTool"], () => {
      this.ruler = new AMap.RangingTool(map);
      this.ruler.turnOn();
      // console.log(this.ruler.)
    });
纪泽龙's avatar
纪泽龙 committed
787
    this.mouseTool.polyline({
纪泽龙's avatar
纪泽龙 committed
788
      // bubbles:false,
纪泽龙's avatar
1  
纪泽龙 committed
789
      strokeWeight: 4,
纪泽龙's avatar
纪泽龙 committed
790 791 792
      strokeColor: "#80d8ff",
      extData: {
        type: "newLine",
793
        //当前line状态 0:正常状态 1:正在编辑状态
纪泽龙's avatar
纪泽龙 committed
794 795 796 797
        isState: 0
      }
      //同Polyline的Option设置
    });
纪泽龙's avatar
纪泽龙 committed
798 799
    // 让它不再等于null
    // this.newLineObj={a:123}
纪泽龙's avatar
纪泽龙 committed
800
  }
纪泽龙's avatar
纪泽龙 committed
801 802 803 804 805
  // 创建出来的新线归位,重置,当点击编辑跟删除的时候
  newLineReset() {
    this.lineFlag = false;
    if (this.mouserTool) {
      this.mouseTool.close();
806 807 808
      if (this.lengthMarker) {
        this.myMap.remove(this.lengthMarker);
      }
纪泽龙's avatar
纪泽龙 committed
809 810
    }
    if (this.newLineObj) {
纪泽龙's avatar
纪泽龙 committed
811
      this.newLineObj.polyEditor.close();
纪泽龙's avatar
纪泽龙 committed
812
      map.remove(this.newLineObj);
纪泽龙's avatar
纪泽龙 committed
813
      this.newLineObj = null;
纪泽龙's avatar
纪泽龙 committed
814 815
    }
  }
纪泽龙's avatar
纪泽龙 committed
816
  // 新建line增加编辑以及右键菜单
纪泽龙's avatar
纪泽龙 committed
817
  closeInfoWindow() {
纪泽龙's avatar
纪泽龙 committed
818
    this.showInfoWindow && this.showInfoWindow.close();
纪泽龙's avatar
纪泽龙 committed
819
    this.markerInfoWindow && this.markerInfoWindow.close();
纪泽龙's avatar
纪泽龙 committed
820 821 822 823
  }
  // 新line与老line添加点击事件
  newLineAddEvent(obj) {
    obj.polyEditor = new AMap.PolyEditor(map, obj);
纪泽龙's avatar
纪泽龙 committed
824 825 826
    // obj.polyEditor.on('adjust',(e)=>{
    //   console.log("addnode")
    // })
827

纪泽龙's avatar
纪泽龙 committed
828 829
    obj.on("mousedown", e => {
      console.log(e.originEvent);
纪泽龙's avatar
纪泽龙 committed
830
      console.log("lineType", this.lineType);
纪泽龙's avatar
纪泽龙 committed
831 832
      this.lineFlag = true;
      // 每次点击关闭然后再次激活
纪泽龙's avatar
纪泽龙 committed
833 834 835 836
      // 如果是新线就关闭
      if (obj.getExtData().type == "newLine") {
        this.mouseTool.close();
      }
纪泽龙's avatar
纪泽龙 committed
837 838 839 840 841
      // 删除
      if (this.lineType == 3) {
        this.lineDelete(obj);
        return;
      }
纪泽龙's avatar
纪泽龙 committed
842
      // 获取当前状态 0普通状态,1是正编辑状态
纪泽龙's avatar
纪泽龙 committed
843
      const { isState, type } = obj.getExtData();
纪泽龙's avatar
纪泽龙 committed
844
      // 如果不是新线的时候并且没点编辑,点击是无效的
纪泽龙's avatar
纪泽龙 committed
845
      if (type != "newLine" && this.lineType != 2) return;
纪泽龙's avatar
纪泽龙 committed
846 847 848 849 850 851 852 853
      if (isState == 0) {
        // 如果是0,就打开编辑,变成编辑状态
        obj.polyEditor.open();
        let opstions = obj.getExtData();
        opstions.isState = 1;
        console.log(opstions);
        obj.setExtData(opstions);
      } else {
纪泽龙's avatar
纪泽龙 committed
854
        // 经纬度
纪泽龙's avatar
纪泽龙 committed
855
        const lnglatsArr = obj.getPath().map(item => [item.lng, item.lat]);
纪泽龙's avatar
纪泽龙 committed
856 857 858 859
        // 管道总长度
        const pipeLength = obj.getLength();
        // 传回来的数据 如果是新管道就是空
        const lineData = type == "newLine" ? {} : obj.getExtData().lineData;
纪泽龙's avatar
纪泽龙 committed
860
        // const lineData =  obj.getExtData().lineData;
纪泽龙's avatar
纪泽龙 committed
861 862 863 864 865 866 867 868
        console.log("lineData===================>传入组件的数据", lineData);
        this.infoWindowPipelineView({
          target: obj,
          lineType: type,
          lnglatsArr,
          pipeLength,
          lineData
        });
纪泽龙's avatar
纪泽龙 committed
869 870 871
      }
    });
  }
纪泽龙's avatar
纪泽龙 committed
872
  // 隐藏所有管道
纪泽龙's avatar
纪泽龙 committed
873 874 875 876 877 878
  lineShow(bool, pipePressureArr) {
    const filterArr = this.polyLines.filter(
      item =>
        pipePressureArr.indexOf(+item.getExtData().lineData.pipePressure) >= 0
    );
    // 然后过滤出来的管道才会显示隐藏
纪泽龙's avatar
纪泽龙 committed
879
    bool
纪泽龙's avatar
纪泽龙 committed
880 881
      ? filterArr.forEach(item => item.show())
      : filterArr.forEach(item => item.hide());
纪泽龙's avatar
纪泽龙 committed
882 883 884 885 886 887 888 889 890 891 892 893 894
    if (this.newLineObj) {
      bool ? this.newLineObj.show() : this.newLineObj.hide();
    }
  }
  markerShow(type, bool) {
    this.markers.forEach(item => {
      const { deviceType } = item.getExtData();
      if (deviceType == type) {
        bool ? item.show() : item.hide();
      }
      // console.log("deviceType",deviceType);
    });
  }
895
  // 其实是修改了状态,不是真的删除
纪泽龙's avatar
纪泽龙 committed
896
  lineDelete(obj) {
纪泽龙's avatar
纪泽龙 committed
897
    const {
898
      lineData: { pipeName, deviceInfoList }
纪泽龙's avatar
纪泽龙 committed
899
    } = obj.getExtData();
900 901 902 903 904 905 906 907 908 909
    let devices = " ";
    let content;
    if (deviceInfoList) {
      for (var i = 0; i < deviceInfoList.length; i++) {
        var item = deviceInfoList[i];
        devices = devices + item.deviceName + " ";
      }
      content = `请确认是否删除管道名称为 + ${pipeName} +的数据项,该管道下包含的设备( ${devices}  )将一并删除`;
    } else {
      content = `请确认是否删除管道名称为 ${pipeName} 的数据项,该管道下不包含任何设备`;
910
    }
纪泽龙's avatar
纪泽龙 committed
911
    vue
912 913 914 915 916 917
      .$confirm(content, "提示", {
        // confirmButtonText: "确定",
        // cancelButtonText: "取消",
        type: "warning"
        // center: true
      })
纪泽龙's avatar
纪泽龙 committed
918
      .then(() => {
919 920 921 922 923 924 925 926 927 928 929
        const { lineData } = obj.getExtData();
        // updatePipe
        // delPipe(pipeId).then(res => {
        let upDataObj = { ...lineData };
        upDataObj.isDel = 1;
        updatePipe(upDataObj).then(res => {
          if (res.code == 200) {
            const index = this.polyLines.indexOf(obj);
            // 如果是老线,就要线删除原来的,然后重新包装一遍
            if (index >= 0) {
              this.polyLines.splice(index, 1);
930 931 932 933 934 935 936 937 938 939 940 941 942 943
              // 删除管道的时候,与管道管理额的设备也要删除,
              // 如果管道存在下级设备,也一并删除
              if (deviceInfoList) {
                for (var i = 0; i < deviceInfoList.length; i++) {
                  const item = deviceInfoList[i];
                  for (let j = 0; j < this.markers.length; j++) {
                    const item2 = this.markers[j];
                    if (item.deviceId == item2.getExtData().deviceId) {
                      map.remove(item2);
                      const ind = this.markers.indexOf(item2);
                      if (ind >= 0) {
                        this.markers.splice(ind, 1);
                      }
                      break;
944 945 946 947 948
                    }
                  }
                }
              }
              map.remove(obj);
949
              // 列表变化
950
              const pipeArr = this.polyLines.map(item => {
951
                return item.getExtData().lineData;
952
              });
953 954 955
              const markerArr = this.markers.map(item => {
                return item.getExtData();
              });
956
              this.view.pipeClassify(pipeArr);
957 958
              this.view.deviceClassify(markerArr);
              this.view.pipeList();
959 960 961 962 963 964 965 966 967 968
            }
            vue.$message({
              type: "success",
              offset: 100,
              // center:true,
              message: "删除成功!"
            });
            // 关闭当前线条的infowindow
            // this.closeLineInfoWindow();
            console.log("改变线条");
纪泽龙's avatar
纪泽龙 committed
969 970 971
          }
        });
      })
972 973
      .catch(err => {
        console.log(err);
纪泽龙's avatar
纪泽龙 committed
974 975
        vue.$message({
          type: "info",
纪泽龙's avatar
纪泽龙 committed
976 977
          // center:true,
          offset: 100,
纪泽龙's avatar
纪泽龙 committed
978 979 980 981
          message: "已取消删除"
        });
      });
  }
纪泽龙's avatar
纪泽龙 committed
982

纪泽龙's avatar
纪泽龙 committed
983 984 985 986
  // 关闭所有已经上传的线的编辑状态
  linePolyEditorAllClose() {
    this.polyLines.forEach(item => {
      item.polyEditor.close();
纪泽龙's avatar
纪泽龙 committed
987
      let opstions = item.getExtData();
988 989
      const { pipePressure } = opstions.lineData;

纪泽龙's avatar
纪泽龙 committed
990 991 992 993
      opstions.isState = 0;
      item.setExtData(opstions);

      let attr = item.getOptions();
994
      // attr.strokeColor = "#F7FE38";
995
      attr.strokeColor = lineColor[pipePressure];
纪泽龙's avatar
纪泽龙 committed
996
      item.setOptions(attr);
纪泽龙's avatar
纪泽龙 committed
997 998 999
    });
  }

纪泽龙's avatar
纪泽龙 committed
1000
  // 传进组件的会调 点确认的时候调
纪泽龙's avatar
纪泽龙 committed
1001
  lineOkCallBack(target, data, func) {
纪泽龙's avatar
纪泽龙 committed
1002 1003 1004
    target.polyEditor.close();
    const index = this.polyLines.indexOf(target);
    // 如果是老线,就要线删除原来的,然后重新包装一遍
纪泽龙's avatar
纪泽龙 committed
1005
    if (index >= 0) {
纪泽龙's avatar
纪泽龙 committed
1006 1007
      // 记录当前位置的元素
      this.changePolineListNum = index;
纪泽龙's avatar
纪泽龙 committed
1008
      this.polyLines.splice(index, 1);
耿迪迪's avatar
耿迪迪 committed
1009
    }
纪泽龙's avatar
纪泽龙 committed
1010
    console.log("data999-=================>传回来的data", data);
纪泽龙's avatar
纪泽龙 committed
1011
    this.addPolyline([data], func);
纪泽龙's avatar
纪泽龙 committed
1012
    map.remove(target);
纪泽龙's avatar
纪泽龙 committed
1013

纪泽龙's avatar
纪泽龙 committed
1014
    // console.log('polyLines',this.polyLines)
耿迪迪's avatar
耿迪迪 committed
1015
  }
纪泽龙's avatar
纪泽龙 committed
1016
  // 上传服务器用的组件
纪泽龙's avatar
纪泽龙 committed
1017
  infoWindowPipelineView(options) {
纪泽龙's avatar
纪泽龙 committed
1018
    // const {} =options;
纪泽龙's avatar
纪泽龙 committed
1019 1020
    const notice = createPop(pipelineView, {
      title: "管道",
纪泽龙's avatar
纪泽龙 committed
1021 1022 1023 1024 1025 1026 1027 1028
      // 数据
      // lineData: options.lineData,
      // lnglatsArr:options.lnglatsArr,
      // pipeLength:options.pipeLength,
      // //线是新线还是老线
      // lineType: options.lineType,
      // target: options.obj,
      ...options,
纪泽龙's avatar
纪泽龙 committed
1029
      //把当前对象当作that传进去
纪泽龙's avatar
纪泽龙 committed
1030 1031
      gaodeMap: this,
      lineOkCallBack: this.lineOkCallBack
纪泽龙's avatar
纪泽龙 committed
1032 1033 1034
    });
    notice.show();
  }
耿迪迪's avatar
耿迪迪 committed
1035 1036 1037
  /**
   * 添加鼠标事件
   */
纪泽龙's avatar
纪泽龙 committed
1038
  addMouseTool() {
耿迪迪's avatar
耿迪迪 committed
1039
    this.mouseTool = new AMap.MouseTool(map);
耿迪迪's avatar
耿迪迪 committed
1040
    //监听draw事件可获取画好的覆盖物
耿迪迪's avatar
耿迪迪 committed
1041 1042
    this.overlays = [];
    let that = this;
耿迪迪's avatar
耿迪迪 committed
1043
    this.markerOverlays = [];
1044
    this.mouseTool.on("draw", function(e) {
1045 1046 1047 1048
      // 如果正在测量就删掉
      if (that.lengthMarker) {
        that.myMap.remove(that.lengthMarker);
      }
纪泽龙's avatar
纪泽龙 committed
1049
      if (e.obj.getExtData().type != "newLine") {
耿迪迪's avatar
耿迪迪 committed
1050
        map.remove(that.markerOverlays);
1051
        const device = that.createInfowindow("新增");
耿迪迪's avatar
耿迪迪 committed
1052 1053 1054
        device.map = map;
        device.obj = e.obj;
        device.gaoMap = that;
1055
        e.obj.on("click", function(aa) {
纪泽龙's avatar
纪泽龙 committed
1056
          let postion = aa.target._position;
1057
          //兼容拖拽后单击事件,拖拽后点击事件返回位置为数组
纪泽龙's avatar
纪泽龙 committed
1058
          if (postion instanceof Array) {
1059 1060
            device.form.longitude = postion[0];
            device.form.latitude = postion[1];
纪泽龙's avatar
纪泽龙 committed
1061
          } else {
1062 1063 1064
            device.form.longitude = postion.lng;
            device.form.latitude = postion.lat;
          }
纪泽龙's avatar
纪泽龙 committed
1065
          device.show();
纪泽龙's avatar
纪泽龙 committed
1066
        });
1067
        e.obj.on("mouseover", function() {
1068 1069
          that.mouseTool.close();
        });
1070
        e.obj.on("mouseout", function() {
1071
          that.draw(that.deviceType);
1072
        });
耿迪迪's avatar
耿迪迪 committed
1073
        that.markerOverlays.push(e.obj);
纪泽龙's avatar
纪泽龙 committed
1074
      }
1075

纪泽龙's avatar
纪泽龙 committed
1076 1077
      if (e.obj && e.obj.getExtData().type == "newLine") {
        console.log("挂上事件");
1078 1079 1080
        // that.mouseTool.overlays.polyLine = [];
        // 清空当前算距离用的内部数组
        that.mouseTool.overlays.polyline = [];
纪泽龙's avatar
纪泽龙 committed
1081
        that.newLineAddEvent(e.obj);
1082 1083 1084
        // 测距结束
        that.ruler.turnOff(true);
        // console.log(e.obj)
1085 1086 1087 1088 1089 1090
        e.obj.on("mouseover", () => {
          e.obj.setOptions({ strokeColor: "#F7FE38" });
        });
        e.obj.on("mouseout", () => {
          e.obj.setOptions({ strokeColor: "#80d8ff" });
        });
纪泽龙's avatar
纪泽龙 committed
1091
        // 记录这条线
纪泽龙's avatar
纪泽龙 committed
1092 1093
        // console.log(e.obj.getPath().length)
        // 如果只有一个点,并没有连成线的时候就不close 大于一个点的时候才执行clse
1094
        // console.log(that.lineType);
纪泽龙's avatar
纪泽龙 committed
1095 1096
        if (e.obj.getPath().length > 1) {
          that.newLineObj = e.obj;
1097 1098

          that.ruler.turnOff(true);
纪泽龙's avatar
纪泽龙 committed
1099 1100 1101 1102
          that.mouseTool.close();
        } else {
          // that.newLineObj = null;
        }
纪泽龙's avatar
纪泽龙 committed
1103
        // 关闭
纪泽龙's avatar
纪泽龙 committed
1104 1105 1106
        // 创建一条新线,然后在点地图的时候删除原来的旧线
        // that.createNewLine();

纪泽龙's avatar
纪泽龙 committed
1107 1108 1109
        // 右键菜单
        // that.addEditorAndMenu(e.obj)
      }
耿迪迪's avatar
耿迪迪 committed
1110

纪泽龙's avatar
纪泽龙 committed
1111 1112 1113
      // if(e.obj.getExtData().type=="line"){
      //   return;
      // }
耿迪迪's avatar
耿迪迪 committed
1114
      //that.mouseTool.close();
纪泽龙's avatar
纪泽龙 committed
1115
      that.overlays.push(e.obj);
耿迪迪's avatar
耿迪迪 committed
1116
    });
耿迪迪's avatar
耿迪迪 committed
1117
  }
耿迪迪's avatar
耿迪迪 committed
1118

1119 1120 1121
  /**
   * 创建弹框事件
   */
1122
  createInfowindow(val) {
纪泽龙's avatar
纪泽龙 committed
1123 1124
    switch (this.deviceType) {
      case DEVICE_TYPE.PIPEPLINE: {
1125 1126
        break;
      }
纪泽龙's avatar
纪泽龙 committed
1127
      case DEVICE_TYPE.REGEULATORBOX: {
1128
        return createPop(regulatorBox, {
1129
          title: val + "调压箱"
1130 1131
        });
      }
纪泽龙's avatar
纪泽龙 committed
1132
      case DEVICE_TYPE.VALUEWELL: {
1133
        return createPop(valveWell, {
1134
          title: val + "阀门井"
1135 1136
        });
      }
纪泽龙's avatar
纪泽龙 committed
1137
      case DEVICE_TYPE.FLOWMETER: {
1138
        return createPop(flowMeter, {
1139
          title: val + "流量计"
1140 1141
        });
      }
1142
      case DEVICE_TYPE.PRESSUREGAGE: {
纪泽龙's avatar
纪泽龙 committed
1143
        return createPop(pressureGage, {
1144
          title: val + "压力表"
1145 1146
        });
      }
1147
    }
耿迪迪's avatar
耿迪迪 committed
1148
  }
耿迪迪's avatar
耿迪迪 committed
1149

1150 1151 1152 1153
  /**
   * 鼠标画图事件
   * @param deviceType 设备类型
   */
纪泽龙's avatar
纪泽龙 committed
1154
  draw(deviceType) {
耿迪迪's avatar
耿迪迪 committed
1155
    let that = this;
1156
    that.deviceType = deviceType;
纪泽龙's avatar
纪泽龙 committed
1157 1158 1159
    if (
      DEVICE_TYPE.REGEULATORBOX == deviceType ||
      DEVICE_TYPE.VALUEWELL == deviceType ||
1160 1161
      DEVICE_TYPE.FLOWMETER == deviceType ||
      DEVICE_TYPE.PRESSUREGAGE == deviceType
纪泽龙's avatar
纪泽龙 committed
1162
    ) {
1163 1164 1165 1166
      that.mouseTool.marker({
        draggable: true
      });
    }
纪泽龙's avatar
纪泽龙 committed
1167
    if (DEVICE_TYPE.PIPEPLINE == deviceType) {
1168 1169
      that.mouseTool.polyline({
        strokeWeight: 9,
纪泽龙's avatar
纪泽龙 committed
1170
        strokeColor: "#80d8ff"
1171 1172 1173
        //同Polyline的Option设置
      });
    }
耿迪迪's avatar
耿迪迪 committed
1174 1175
  }

耿迪迪's avatar
耿迪迪 committed
1176 1177 1178
  /**
   * 添加地图控件
   */
纪泽龙's avatar
纪泽龙 committed
1179 1180 1181 1182 1183 1184 1185 1186 1187
  addMapControl() {
    AMap.plugin(
      [
        "AMap.ToolBar",
        "AMap.Scale",
        "AMap.HawkEye",
        "AMap.MapType",
        "AMap.Geolocation"
      ],
1188
      function() {
纪泽龙's avatar
纪泽龙 committed
1189 1190
        // 在图面添加工具条控件,工具条控件集成了缩放、平移、定位等功能按钮在内的组合控件
        //map.addControl(new AMap.ToolBar());
耿迪迪's avatar
耿迪迪 committed
1191

纪泽龙's avatar
纪泽龙 committed
1192 1193
        // 在图面添加比例尺控件,展示地图在当前层级和纬度下的比例尺
        map.addControl(new AMap.Scale());
耿迪迪's avatar
耿迪迪 committed
1194

纪泽龙's avatar
纪泽龙 committed
1195 1196
        // 在图面添加鹰眼控件,在地图右下角显示地图的缩略图
        map.addControl(new AMap.HawkEye({ isOpen: true }));
耿迪迪's avatar
耿迪迪 committed
1197

纪泽龙's avatar
纪泽龙 committed
1198 1199 1200 1201
        // 在图面添加类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
        map.addControl(
          new AMap.MapType({ position: { top: "10px", left: "100px" } })
        );
耿迪迪's avatar
耿迪迪 committed
1202

纪泽龙's avatar
纪泽龙 committed
1203 1204 1205 1206
        // 在图面添加定位控件,用来获取和展示用户主机所在的经纬度位置
        // map.addControl(new AMap.Geolocation());
      }
    );
耿迪迪's avatar
耿迪迪 committed
1207
  }
1208

纪泽龙's avatar
纪泽龙 committed
1209
  searchTips(inputId) {
1210
    let that = this;
1211
    AMap.plugin(["AMap.AutoComplete", "AMap.PlaceSearch"], function() {
1212
      //输入提示
纪泽龙's avatar
z  
纪泽龙 committed
1213 1214 1215 1216
      // var autoOptions = {
      //   input: inputId
      // };
      // let auto = new AMap.AutoComplete(autoOptions);
1217 1218 1219 1220
      that.placeSearch = new AMap.PlaceSearch({
        map: map
      });
      //构造地点查询类
纪泽龙's avatar
z  
纪泽龙 committed
1221 1222 1223 1224
      // auto.on("select", function(e) {
      //   that.searchSelectAdcode = e.poi.adcode;
      //   that.searchSelectName = e.poi.name;
      // });
纪泽龙's avatar
纪泽龙 committed
1225
    });
1226 1227
  }

纪泽龙's avatar
纪泽龙 committed
1228
  closeAddMarker() {
耿迪迪's avatar
耿迪迪 committed
1229 1230
    this.mouseTool.close();
    map.remove(this.markerOverlays);
1231 1232 1233
    if (this.lengthMarker) {
      this.myMap.remove(this.lengthMarker);
    }
耿迪迪's avatar
耿迪迪 committed
1234
  }
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
  // 只上图没功能
  onlyLine(obj) {
    const { coordinates, pipeName } = obj;
    let polyline = new AMap.Polyline({
      path: eval(coordinates),
      strokeColor: "#80d8ff",
      strokeWeight: 4,
      strokeOpacity: 0.9,
      zIndex: 50,
      bubble: true,
      geodesic: true,
      extData: {
        type: "line",
        //当前line状态 0:正常状态 1:正在编辑状态
        isState: 0,
        lineData: obj
      }
    });
    let infoWindow = new AMap.InfoWindow({
1254
      isCustom: true,
yaqizhang's avatar
yaqizhang committed
1255
      content: `<div style='max-width:150px;border:1px solid #80d8ff;background:#fff'>${pipeName}</div>`,
1256
      anchor: "left-top",
1257
      offset: new AMap.Pixel(20, -20)
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
    });
    polyline.on("mouseover", e => {
      const options = polyline.getOptions();
      options.strokeColor = "#FF5A67";
      polyline.setOptions(options);
      infoWindow.open(map, e.lnglat);
    });
    polyline.on("mouseout", e => {
      const options = polyline.getOptions();
      options.strokeColor = "#80d8ff";
      polyline.setOptions(options);
      infoWindow.close();
    });
    map.add(polyline);
yaqizhang's avatar
yaqizhang committed
1272 1273
    const path1 = eval(coordinates)[0];
    const path2 = eval(coordinates)[1];
1274
    // map.setFitView();
1275 1276 1277 1278
    const path3 = [
      (Number(path1[0]) + Number(path2[0])) / 2,
      (Number(path1[1]) + Number(path2[1])) / 2
    ];
yaqizhang's avatar
yaqizhang committed
1279
    map.setCenter(path3, false);
1280 1281 1282 1283 1284 1285

    // const lntlat = eval(coordinates)[0];
    // map.panTo(lntlat)

    // 信息窗体
  }
耿迪迪's avatar
耿迪迪 committed
1286 1287
}
export default gaodeMap;