index.vue 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<template>
  <div style="position: relative">
    <div class="search-wrapper pos">
      <el-input
        v-model="searchinput"
        class="searchinput"
        placeholder="请输入内容"
        size="mini"
        style="width: 150px"
        ref="input"
      ></el-input>
    </div>
    <div
      id="drawArea"
      :style="{height: this.height + 'px'}"
    ></div>
  </div>

</template>

<script>
  import { EditorMap } from "@/utils/mapClass/getPath.js";
23
import { _ } from "core-js";
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  export default {
    name: "draw-area",
    props:{
      value: {
        type: Array,
        default: ()=>[],
      },
      height:{
        type: Number,
        default: 500
      }
    },
    data(){
        return{
          gaoMap: null,
          mouseTool: null,
40
          drawLayers: [],
41 42 43 44 45 46 47 48 49 50 51 52
          overlays: null,
          firstClick: 0,
          nowMouseTarget: null,
          currentPolygonId: 0,
          isClickPolygonFlag: false,
          opts:{
            fillColor:'#00b0ff',
            strokeColor:'#80d8ff'
          },
          searchinput: ""
        }
    },
53
    mounted(){ 
54 55 56 57 58 59
      this.initMap();
    },
    methods:{
      //地图初始化
      initMap(){
        const path = eval(this.$store.state.user.systemSetting.map_center);
60
        this.gaoMap = new EditorMap("drawArea", {center:path}, this); 
61 62
        if(this.value.length > 0){
          this.loadArea();
63 64
        } 
        this.initMouseEvent();
65 66
      },
      //初始化鼠标事件
67
      initMouseEvent(){ 
68
        let that = this;
69
        this.gaoMap.map.plugin(["AMap.MouseTool"], () => {
70 71
          that.mouseTool = new AMap.MouseTool(this.gaoMap.map);
          that.mouseTool.polygon(that.opts);
72 73 74
          that.mouseTool.on('draw',function(e){   
            let nowMouseTargetObj = new Object();
            nowMouseTargetObj= e.obj;
75
                    // 如果只允许画一个则用这个方法 发数据
76
            // that.$emit("input", that.nowMouseTarget._opts.path);
77
            that.isClickPolygonFlag = false;
78
            if(nowMouseTargetObj._opts.path.length > 2){
79 80 81 82
              that.mouseTool.close();
              that.isClickPolygonFlag = true;
            }
            that.firstClick = 0;
83
            nowMouseTargetObj.on('click',function () {
84 85
              that.isClickPolygonFlag = true;
              that.firstClick++;
86
              // if(that.firstClick < 2){
87 88
                that.gaoMap.map.plugin(["AMap.PolygonEditor"],(n) => {
                  const polygonEditor = new AMap.PolygonEditor(that.gaoMap.map,e.obj);
89
                  nowMouseTargetObj.polygonEditor = polygonEditor;
90
                  polygonEditor.on("removenode", (e) => { 
91
                    that.isClickPolygonFlag = true;
92
                    if(nowMouseTargetObj._opts.path.length == 0){
93 94 95 96 97 98
                      setTimeout(() => {
                        that.mouseTool.polygon(that.opts);
                      }, 230);
                    }
                  });
                  polygonEditor.on("adjust",(e) =>{
99
                    that.$emit("update-path", that.drawLayers); 
100 101
                  })
                  polygonEditor.on("addnode",(e) =>{
102
                    that.$emit("update-path", that.drawLayers); 
103 104
                  })
                  polygonEditor.open();
105
                });
106
              // }
107
            });
108
            nowMouseTargetObj.on("mouseout", (e) => {
109
              that.isClickPolygonFlag = false;
110 111 112 113 114
              that.mouseTool.polygon(that.opts);
            })

            // 鼠标放到了这个上面
            nowMouseTargetObj.on("mouseover", (e) => { 
115
              that.mouseTool && that.mouseTool.close();
116 117
            })
            //右键关闭区域编辑
118
            nowMouseTargetObj.on("rightclick",(e) => {
119
              that.firstClick = 0;
120
              nowMouseTargetObj.polygonEditor.close();
121 122
            })

123 124 125 126 127 128 129 130 131 132
            // 双击删除
            nowMouseTargetObj.on("dblclick",(e) => {    
              that.$confirm("是否删除此区域", { type: "warning" })
                .then(() => { 
                  let deleteIndex = null;
                  for (let i = 0;i < that.drawLayers.length;i ++) {       
                    var isPointInRing = AMap.GeometryUtil.isPointInRing(e.lnglat,that.drawLayers[i]._opts.path);
                    if (isPointInRing) {
                      deleteIndex = i;
                    }
133
                  }
134 135 136 137 138 139
                  if (deleteIndex != null) {
                    that.drawLayers[deleteIndex].polygonEditor.close();
                    that.gaoMap.map.remove(that.drawLayers[deleteIndex]);
                    that.drawLayers.splice(deleteIndex, 1);
                  }
                }).catch(() => {});
140
                that.$emit("update-path", that.drawLayers); 
141 142
            })

143
            if (nowMouseTargetObj._opts.path.length > 2) {
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
              
              let haveObjNum = null;
              if (that.drawLayers.length > 0) {
                for (let i = 0; i < that.drawLayers.length; i++) {
                  let item  = that.drawLayers[i];
                  // 是后来新添加的
                  if (item._opts.path) {
                    let optsPathItem = item._opts.path;
                    if (JSON.stringify(optsPathItem) === JSON.stringify(nowMouseTargetObj._opts.path)) {
                      haveObjNum = i;
                    }
                  // 是之前回显的数据
                  } 
                  
                  if (item.getPath()) {
                    let pointsItem = item.getPath();
                    if (JSON.stringify(pointsItem) === JSON.stringify(nowMouseTargetObj._opts.path)) {
                      haveObjNum = i;
                    }
                  }
                }
              }
              if (haveObjNum == null) {
                that.$set(that.drawLayers, that.drawLayers.length, nowMouseTargetObj);
              }
169
            }
170
            that.$emit("update-path", that.drawLayers); 
171
            that.mouseTool.polygon(that.opts);
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
      },  
      initMapPolygon(value) {
        let that = this; 
        let haveObjNum = null; 
        if (that.drawLayers.length > 0) {
          for (let i = 0; i < that.drawLayers.length; i++) {
            let item  = that.drawLayers[i]; 
            // 是后来新添加的
            if (item._opts.path) {
              let optsPathItem = item._opts.path;
              if (JSON.stringify(optsPathItem) === JSON.stringify(value)) {
                haveObjNum = i;
              }
            // 是之前回显的数据
            } 
            
            if (item.getPath()) { 
              if (JSON.stringify(item.getPath()) === JSON.stringify(value)) {
                haveObjNum = i;
              }
            }
          }
        }
        if (haveObjNum != null) {
          return;
        }
 
201
        const polygon = new AMap.Polygon({
202 203
          map:that.gaoMap.map,
          path: value,
204
          fillColor:'#00b0ff',
205 206
          strokeColor:'#80d8ff',
          extData: {id: that.drawLayers.length}
207
        });
208 209 210 211 212 213 214 215 216 217 218
        let mouseTarget = new Object();
        mouseTarget = polygon;
        that.firstClick = 0;
        mouseTarget.on("click",(e) =>{
          that.isClickPolygonFlag = true;
          that.firstClick++;
          if(that.firstClick < 2){
            that.gaoMap.map.plugin(["AMap.PolygonEditor"],()=> {
              mouseTarget.polygonEditor = new AMap.PolygonEditor(that.gaoMap.map,e.target);
              mouseTarget.polygonEditor.on("removenode", (e) => {
                that.isClickPolygonFlag = true;
219
              });
220 221
              mouseTarget.polygonEditor.on("adjust",(e) =>{  
                that.$emit("update-path", that.drawLayers); 
222
              })
223 224 225
              mouseTarget.polygonEditor.on("addnode",(e) =>{
                that.$emit("update-path", that.drawLayers); 

226
              })
227
              mouseTarget.polygonEditor.open();
228 229 230
          })
          }
        });
231 232
        mouseTarget.on("mouseout", (e) => {
          that.isClickPolygonFlag = false;
233
        })
234 235 236
        mouseTarget.on("rightclick",() => {
          that.firstClick = 0;
          mouseTarget.polygonEditor && mouseTarget.polygonEditor.close();
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
        mouseTarget.on("mouseout", (e) => {
          that.isClickPolygonFlag = false;
          that.mouseTool.polygon(that.opts);
        })

        // 鼠标放到了这个上面
        mouseTarget.on("mouseover", (e) => { 
          that.mouseTool && that.mouseTool.close();
        })
        // 双击删除
        mouseTarget.on("dblclick",(e) => {    
          that.firstClick = 0;
          that.$confirm("是否删除此区域", { type: "warning" })
            .then(() => { 
              let deleteIndex = null;
              for (let i = 0;i < that.drawLayers.length;i ++) {       
                if (that.drawLayers[i]._opts.path) {
                  var isPointInRing = AMap.GeometryUtil.isPointInRing(e.lnglat,that.drawLayers[i]._opts.path);
                  if (isPointInRing) {
                    deleteIndex = i;
                  }
                }
                if (that.drawLayers[i].getPath()) {
                  var isPointInRing = AMap.GeometryUtil.isPointInRing(e.lnglat,that.drawLayers[i].getPath());
                  if (isPointInRing) {
                    deleteIndex = i;
                  }
                }
              } 
              if (deleteIndex != null) { 

                if (that.drawLayers[deleteIndex].polygonEditor) {
                  that.drawLayers[deleteIndex].polygonEditor.close(); 
                }
                that.gaoMap.map.remove(that.drawLayers[deleteIndex]); 
                that.drawLayers.splice(deleteIndex, 1); 
              }
              that.$emit("update-path", that.drawLayers); 
            }).catch(() => {});
        })


        if (haveObjNum === null) {
          that.$set(that.drawLayers, that.drawLayers.length, mouseTarget); 
        }
      },
      //区域回显
      loadArea(){
        if (this.value.length > 1) {
          for (let i = 0;i < this.value.length;i++) { 
            this.initMapPolygon(this.value[i]);
          }
          this.gaoMap.map.setZoom(12) 
        } else if(this.value.length === 1) {
            this.initMapPolygon(this.value[0]);
            this.gaoMap.setCenter(this.value[0][0]);
            this.gaoMap.map.setZoom(12) 
        }
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
      }
    }
  }
</script>

<style scoped lang="scss">
  .search-wrapper {
    left: 30px;
  }
  .positionBtn {
    right: 30px;
  }
  .pos {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 20;
  }
  #drawArea {
    width: 100%;
    border: 1px solid;
  }

</style>