Administrator
2022-04-11 79d9fc857559982b00b68b2d709807bdc4cd286f
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
<!--
 * @Description: 绘制图形统计圈内点数
 * @Author: Dragon
 * @Date: 2020-12-21 13:19:47
 * @LastEditTime: 2020-12-21 15:03:27
 * @LastEditors: Dragon
-->
 
<template>
  <div>
    <div class="query-wrap">
      <el-button type="primary" @click="drawStart('Polygon')">
        {{ isDraw ? "绘制区域查询人数" : "重新绘制" }}
      </el-button>
    </div>
    <div id="map"></div>
  </div>
</template>
 
<script>
import "ol/ol.css";
import { Map, View, Feature } from "ol";
import { Image as ImageLayer, Vector as VectorLayer } from "ol/layer";
import XYZ from "ol/source/XYZ";
import { ImageStatic, Vector as VectorSource } from "ol/source";
import { getCenter } from "ol/extent";
import OlLayerTile from "ol/layer/Tile.js";
import { Projection } from "ol/proj";
import Draw from "ol/interaction/Draw";
import { Point } from "ol/geom";
import { Icon, Style, Text, Fill, Stroke } from "ol/style";
 
import staticMap from "@/assets/img/car.png";
import img from "@/assets/img/car.png";
 
export default {
  data() {
    return {
      map: null, // 地图
      imgx: 0, // 当前地图宽
      imgy: 0, // 当前地图高
      isDraw: true, // 是否绘制
      draw: null,
      source: null,
      vector: null,
      styles: [
        new Style({
          stroke: new Stroke({
            color: "rgba(255,0,0,0.6)",
            width: 2,
          }),
          fill: new Fill({
            color: "rgba(255,0,0,0.3)",
          }),
        }),
      ],
      persons: [],
      polygon: [],
      polygonPersons: [],
      features: [],
      feature: null,
      vectorSource: new VectorSource(),
      timer: null,
    };
  },
  watch: {
    persons(val) {
      if (val) {
        this.setFeature();
      }
    },
  },
  methods: {
    // 初始化地图
    initMap() {
      let extent = [0, 0, this.imgx, this.imgy];
      let projection = new Projection({
        extent: extent,
      });
      let $this = this;
      // 默认地图
      let mapLayer = new OlLayerTile({
          zIndex: 4,
          title: "影像",
          source: new XYZ({
            url: "https://webmap-tile.sf-express.com/MapTileService/rt?fetchtype=static&x={x}&y={y}&z={z}&project=sfmap&pic_size=256&pic_type=png8&data_name=361100&data_format=merged-dat&data_type=normal", // 行政区划
          }),
        });
 
      // 绘制点
      let featureLayer = new VectorLayer({
        source: this.vectorSource,
      });
 
      this.map = new Map({
        target: "map",
        layers: [
          mapLayer, // 展示地图层
          featureLayer, // 点
        ],
        view: new View({
          projection: "EPSG:4326",
          center: [0, 0],
          zoom: 2,
          maxZoom: 13,
        }),
      });
 
       var view = this.map.getView();
 
    // view.setCenter([115.85883507433789, 28.708432053474827])
    view.setCenter([115.9032747077233, 28.67433116990186]);
 
      this.source = new VectorSource({ wrapX: false });
      this.vector = new VectorLayer({
        source: this.source,
        style: this.styles,
      });
      this.map.addLayer(this.vector);
    },
 
    // 点
    setFeature() {
      let that = this;
      that.features = [];
      that.vectorSource.clear();
      that.persons.map((item) => {
        that.feature = new Feature({
          geometry: new Point([item.x, item.y]),
          name: item.name,
        });
        // 设置Feature的样式,使用小旗子图标
        that.feature.setStyle(
          new Style({
            image: new Icon({
              anchor: [0, 1],
              src: img,
            }),
            text: new Text({
              // 位置
              textAlign: "center",
              // 基准线
              textBaseline: "middle",
              // 文字样式
              font: "normal 20px 微软雅黑",
              // 文本内容
              text: item.name,
              // 文本填充样式(即文字颜色)
              fill: new Fill({ color: "#aa3300" }),
              stroke: new Stroke({ color: "#ffcc33", width: 2 }),
            }),
          })
        );
        that.features.push(that.feature);
      });
      that.vectorSource.addFeatures(that.features);
    },
 
    // 开始绘制多边形
    drawStart(type) {
      let that = this;
      if (this.isDraw) {
        this.isDraw = false;
        this.draw = new Draw({
          source: this.source,
          type: type,
        });
        this.map.addInteraction(this.draw);
        this.draw.on("drawend", function (evt) {
          that.drawingEnd(evt);
        });
      } else {
        this.source.clear();
        this.map.removeInteraction(this.draw);
        this.isDraw = true;
        this.polygon = [];
        this.polygonPersons = [];
      }
    },
 
    // 构建多边形结束
    drawingEnd(evt) {
      let that = this;
      const geo = evt.feature.getGeometry();
      const t = geo.getType();
      if (t === "Polygon") {
        // 获取坐标点
        const points = geo.getCoordinates();
        points[0].map((item) => {
          that.polygon.push({ lng: item[0], lat: item[1] });
        });
        console.log(this.polygon,1111);
        for (let i = 0; i < this.persons.length; i++) {
          let boxFlag = this.isRegion(this.persons[i].x, this.persons[i].y, this.polygon);
          if (boxFlag) {
            this.polygonPersons.push(this.persons[i]);
          }
        }
        this.$message({
          type: "success",
          message: `区域内有${this.polygonPersons.length}人`,
        });
        console.warn(this.polygonPersons, `区域内有${this.polygonPersons.length}人`);
        debugger;
        this.map.removeInteraction(this.draw);
      }
    },
 
    // 验证点是否在区域内
    isRegion(lon, lat, aPoints) {
      let num = 0,
        iCount;
      let dLon1, dLon2, dLat1, dLat2, dLon;
      if (aPoints.length < 3) return false;
      iCount = aPoints.length;
      for (let i = 0; i < iCount; i++) {
        if (i == iCount - 1) {
          dLon1 = aPoints[i].lng;
          dLat1 = aPoints[i].lat;
          dLon2 = aPoints[0].lng;
          dLat2 = aPoints[0].lat;
        } else {
          dLon1 = aPoints[i].lng;
          dLat1 = aPoints[i].lat;
          dLon2 = aPoints[i + 1].lng;
          dLat2 = aPoints[i + 1].lat;
        }
        if ((lat >= dLat1 && lat < dLat2) || (lat >= dLat2 && lat < dLat1)) {
          if (Math.abs(dLat1 - dLat2) > 0) {
            dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - lat)) / (dLat1 - dLat2);
            if (dLon < lon) num++;
          }
        }
      }
      if (num % 2 != 0) return true;
      return false;
    },
  },
  mounted() {
    let img = new Image();
    img.src = staticMap;
    let that = this;
    img.onload = function (res) {
      that.imgx = res.target.width;
      that.imgy = res.target.height;
      that.initMap();
    };
  },
  created() {
    let that = this;
    that.timer = setInterval(function () {
      that.persons = [
        { id: 1, name: "点-1", x: 497.08, y: 187.88, z: 0 },
        { id: 2, name: "点-2", x: 725.32, y: 565.88, z: 0 },
        { id: 3, name: "点-3", x: 643.24, y: 503.96, z: 0 },
      ];
    }, 2000);
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>
 
<style>
#map {
  width: 100%;
  height: calc(100vh - 50px);
}
</style>