管道基础大数据平台系统开发-【前端】-新系統界面
1
Surpriseplus
2022-12-21 ec3342e1b34dd02f33dae9bed2db16a14ae8096d
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
<template>
  <div id="mapView" class="projectBox">
    <div class="Tools">
      <el-button
        type="success"
        icon="el-icon-plus"
        @click="drowPoint"
      ></el-button>
      <el-button
        type="danger"
        icon="el-icon-delete"
        @click="clearDrawPoint"
      ></el-button>
    </div>
  </div>
</template>
 
<script>
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import Map from 'ol/Map';
import View from 'ol/View';
import { transform } from 'ol/proj';
import VectorSource from 'ol/source/Vector';
import { Vector as VectorLayer, Tile } from 'ol/layer';
import { Draw } from 'ol/interaction';
import Feature from 'ol/Feature';
import { Circle as CircleStyle, Style, Fill, Stroke } from 'ol/style';
import { Point } from 'ol/geom';
export default {
  data() {
    return {
      mapol: null,
      drawLayer: null,
      draw: null,
    };
  },
  methods: {
    initOlMap() {
      var vectorLayer = new TileLayer({
        source: new XYZ({
          url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
        }),
      });
      this.mapol = new Map({
        target: 'mapView',
        layers: [vectorLayer],
        view: new View({
          center: transform([105.02, 34.9], 'EPSG:4326', 'EPSG:3857'),
          zoom: 4,
          projection: 'EPSG:3857',
        }),
      });
      if (this.$store.state.projeOl != null) {
        this.showPointInMap();
      }
    },
    showPointInMap() {
      var value = this.$store.state.projeOl
        .replace('POINT(', '')
        .replace(')', '');
      var a1 = value.split(' ');
      console.log(a1);
      var a2 = transform(
        [parseFloat(a1[0]), parseFloat(a1[1])],
        'EPSG:4326',
        'EPSG:3857'
      );
      var feature = new Feature({
        geometry: new Point(a2),
      });
      feature.setStyle(
        new Style({
          image: new CircleStyle({
            fill: new Fill({
              color: 'blue',
            }),
            radius: 4,
          }),
        })
      );
      let source = new VectorSource();
      source.addFeature(feature);
      this.drawLayer = new VectorLayer();
      this.drawLayer.setSource(source);
      this.mapol.addLayer(this.drawLayer);
    },
    clearDrawPoint() {
      if (this.drawLayer) {
        this.mapol.removeLayer(this.drawLayer);
        this.drawLayer = null;
        this.$store.state.projeOl = null;
      }
    },
 
    drowPoint() {
      const source = new VectorSource({ wrapX: false });
      if (this.drawLayer) {
        this.mapol.removeLayer(this.drawLayer);
      }
      this.drawLayer = new VectorLayer({
        source: source,
      });
      this.mapol.addLayer(this.drawLayer);
      this.draw = new Draw({
        source: source, // 和图层使用同一个source,画的图形在图层上,图层在地图上即可展示
        type: 'Point', // 可选择三角形,多边形,圆形等具体见官网demo
        freehand: false, // 画选还是点选
      });
      // 添加交互
      this.mapol.addInteraction(this.draw);
      this.draw.on('drawend', (e) => {
        let feature = e.feature;
        let geom = feature.getGeometry();
        var extent = geom.flatCoordinates;
        var a1 = transform([extent[0], extent[1]], 'EPSG:3857', 'EPSG:4326');
 
        this.$store.state.projeOl =
          'POINT(' + a1[0].toFixed(6) + ' ' + a1[1].toFixed(6) + ')';
        this.mapol.removeInteraction(this.draw);
      });
    },
  },
  mounted() {
    this.initOlMap();
  },
};
</script>
 
<style>
.projectBox {
  width: 100%;
  height: 100%;
  overflow: hidden;
  margin: 0;
  padding: 0;
}
.Tools {
  position: absolute;
  background: #303030;
  opacity: 0.9;
  z-index: 40;
  padding: 10px;
  width: 95%;
}
.primary {
  background: #409eff;
  border: #409eff;
  color: white;
}
</style>