ll
liukangdong
2025-03-03 3aa3e034d30b201dde95d9bc33e5b1250dd8f256
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
<template>
  <GlobalWindow title="绘制运营区域" :visible.sync="isShowModal" :confirm-working="isWorking" @close="close"
    @confirm="confirm" width="1000px">
    <div class="map_title">绘制:鼠标左键点击及移动即可绘制图形,绘制过程中按下esc键可中断该过程</div>
    <div class="map_title">鼠标左键双击即可结束当前多边形绘制,多边形会自动闭合;</div>
    <div class="map_wrap">
      <div class="btns">
        <el-button plain type="primary" @click="reset">{{ polygons.length > 0 ? '重新绘制' : '开始绘制' }}</el-button>
        <!-- <el-button plain type="primary" @click="changeMode">结束绘制</el-button> -->
        <!-- <el-button plain @click="reset">重新绘制</el-button> -->
        <!-- <el-button plain  type="danger" @click="deleteObj">删除</el-button> -->
      </div>
      <div id="container" style="width: 100%; height: 100%"></div>
    </div>
  </GlobalWindow>
</template>
 
<script>
import GlobalWindow from '@/components/common/GlobalWindow'
import { Message } from 'element-ui'
var map // 地图
// var editor // 编辑器
export default {
  components: {
    GlobalWindow
  },
  data() {
    return {
      isShowModal: false,
      isWorking: false,
 
      map: null,
      editor: null,
      polygon: null,
      polygons: [],            // 存储所有绘制的多边形
      selectedPolygon: null,
    }
  },
  methods: {
    initMap() {
      map = new TMap.Map("container", {
        zoom: 16, // 设置地图缩放级别
        center: new TMap.LatLng(31.722763, 117.385480) // 设置地图中心点坐标
      })
      var tempList = []
      if (this.polygons.length > 0) {
        tempList = [{ paths: this.polygons.map(item => new TMap.LatLng(item.lat, item.lng)) }]
      }
      this.polygon = new TMap.MultiPolygon({
        map: map,
        id: 'polygon',
        selectedStyleId: 'highlight',
        styles: {
          highlight: new TMap.PolygonStyle({
            color: 'rgba(255, 255, 0, 0.6)'
          })
        },
        geometries: tempList
      })
      this.editor = new TMap.tools.GeometryEditor({
        map,
        overlayList: [{
          overlay: this.polygon,
          id: 'polygon',
        }],
        actionMode: tempList.length > 0 ? TMap.tools.constants.EDITOR_ACTION.INTERACT : TMap.tools.constants.EDITOR_ACTION.DRAW, // 编辑器的工作模式
        activeOverlayId: 'polygon', // 激活图层
        snappable: true, // 开启吸附
        selectable: true,
      })
 
      let evtList = ['delete', 'adjust', 'split', 'union']
      evtList.forEach(evtName => {
        this.editor.on(evtName + '_complete', evtResult => {
          // console.log(evtName, evtResult)
        })
      })
      this.editor.on('draw_complete', (geometry) => {
        var id = geometry.id
        if (this.editor.getActiveOverlay().id === 'polygon') {
          var geo = this.polygon.geometries.filter(function (item) {
            return item.id === id
          })
          this.polygons = geo[0].paths
          // console.log('绘制的多边形坐标:', geo[0].paths)
        }
        this.changeMode()
      })
    },
    handleAgain() {
      this.editor.setActionMode(TMap.tools.constants.EDITOR_ACTION.DRAW)
    },
    reset() {
      // this.editor.enable()
      this.polygons = []
      this.polygon.setGeometries([])
      this.polygon.remove('polygon')
      this.editor.setActionMode(TMap.tools.constants.EDITOR_ACTION.DRAW)
    },
    changeMode() {
      this.editor.setActionMode(TMap.tools.constants.EDITOR_ACTION.INTERACT)
    },
    deleteObj() {
      this.editor.delete()
    },
    close() {
      this.reset()
      map.destroy()
      this.isShowModal = false
    },
    confirm() {
      const list = this.editor.getOverlayList()[0].overlay.geometries
      if (list.length == 0) return Message.warning('请先绘制运营区域')
      this.isShowModal = false
      this.reset()
      this.$emit('change', list[0].paths)
 
 
    }
  }
}
</script>
 
<style lang="scss" scoped>
.map_title {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 6px;
}
 
.map_wrap {
  width: 100%;
  height: calc(100% - 60px);
  position: relative;
 
  .btns {
    position: absolute;
    left: 50%;
    top: 10px;
    transform: translate(-50%, 0);
    z-index: 99999999999;
  }
}
</style>