zhanghua
2025-04-14 829f5116884f98643ffc5b2a548a600d40c0cedb
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
<template>
  <div>
    <div id="container"></div>
    <div class="optionBtn">
      <el-button
        type="primary"
        class="btn submit"
        @click.native.prevent="handleRanging"
        >测距
      </el-button>
      <el-button
        type="primary"
        class="btn submit"
        @click.native.prevent="handleDraw"
        >画线
      </el-button>
      <el-button
        type="primary"
        class="btn submit"
        @click.native.prevent="handleDelete"
        >删除
      </el-button>
    </div>
  </div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
export default {
  data() {
    return {
      map: null,
      mouseTool: null,
      ruler: null,
      _point: { x: "119.27179890", y: "28.59027084" },
      _zoom: 15,
    };
  },
  created() {},
  methods: {
    locationMap() {
      const that = this;
      that.zoom = that.zoom || that._zoom;
      that.point = that.point || that._point;
      that.map.setZoomAndCenter(that.zoom, [that.point.x, that.point.y]);
 
      if (this.region) {
        let polygon = new AMap.Polygon({
          path: eval(this.region),
          fillColor: "#ccebc5",
          strokeOpacity: 1,
          fillOpacity: 0.5,
          strokeColor: "#2b8cbe",
          strokeWeight: 1,
          strokeStyle: "dashed",
          strokeDasharray: [5, 5],
        });
 
        that.map.add(polygon);
      } else {
        that.mouseTool.polygon({
          fillColor: "#00b0ff",
          strokeColor: "#80d8ff",
          //同Polygon的Option设置
        });
      }
    },
    handleRanging() {
      this.ruler.turnOn();
      this.mouseTool.close(false);
    },
    handleDraw() {
      this.ruler.turnOff();
      this.mouseTool.close(false);
      this.mouseTool.polygon({
        fillColor: "#00b0ff",
        strokeColor: "#80d8ff",
        //同Polygon的Option设置
      });
    },
    handleDelete() {
      this.map.clearMap();
    },
  },
  mounted() {
    const that = this;
    AMapLoader.load({
      key: "091ade377d4db40f68cc78cb9658ce8d", // 申请好的Web端开发者Key,首次调用 load 时必填
      version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
      plugins: ["AMap.MouseTool", "AMap.RangingTool"], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
    })
      .then((AMap) => {
        that.map = new AMap.Map("container",{resizeEnable: true});
        that.mouseTool = new AMap.MouseTool(that.map);
        that.mouseTool.on("draw", function (e) {
          //绘制完成后将数组传入表单
          that.$emit("getRegion", e.obj.getOptions());
        });
        //默认样式
        that.ruler = new AMap.RangingTool(that.map);
        that.locationMap();
      })
      .catch((e) => {
        console.log(e);
      });
  },
  watch: {
    point(newval, oldval) {
      this._point = newval;
      this.locationMap();
    },
    zoom(newval, oldval) {
      this._zoom = newval;
    },
  },
  props: ["point", "zoom", "region"],
};
</script>
<style lang="scss" scoped>
#container {
  width: 100%;
  height: 100%;
}
.optionBtn {
  display: flex;
  flex-direction: column;
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 9;
}
.el-button {
  width: 70px;
  margin-bottom: 5px;
  margin-left: 10px;
}
</style>