|
@@ -1,15 +1,24 @@
|
|
|
// 引入OpenLayers的主模块
|
|
|
import Map from 'ol/Map';
|
|
|
import View from 'ol/View';
|
|
|
+import Feature from 'ol/Feature';
|
|
|
+import Point from 'ol/geom/Point';
|
|
|
+import VectorLayer from 'ol/layer/Vector';
|
|
|
+import VectorSource from 'ol/source/Vector';
|
|
|
+import Style from 'ol/style/Style';
|
|
|
+import Icon from 'ol/style/Icon';
|
|
|
+import Text from 'ol/style/Text';
|
|
|
import Projection from 'ol/proj/Projection';
|
|
|
import { getWidth, getTopLeft } from 'ol/extent';
|
|
|
import TileLayer from 'ol/layer/Tile';
|
|
|
import WMTS from 'ol/source/WMTS';
|
|
|
import WMTSTileGrid from 'ol/tilegrid/WMTS';
|
|
|
import WMTSCapabilities from 'ol/format/WMTSCapabilities';
|
|
|
+import { Fill, Stroke } from 'ol/style';
|
|
|
import proj4 from 'proj4';
|
|
|
import { register } from 'ol/proj/proj4';
|
|
|
import { defaults } from 'ol/control';
|
|
|
+import { fromLonLat } from 'ol/proj';
|
|
|
import axios from 'axios';
|
|
|
// import olPlot from 'ol-plot';
|
|
|
// import { activate } from '../ol-plot/ol-plot'
|
|
@@ -31,6 +40,8 @@ for (let z = 2; z < 22; ++z) {
|
|
|
|
|
|
export class olMap {
|
|
|
private map;
|
|
|
+ private options;
|
|
|
+ private markers = [];
|
|
|
private drawOptions = {
|
|
|
graphicsType: 'circle',
|
|
|
strokeColor: '#f80102',
|
|
@@ -45,8 +56,10 @@ export class olMap {
|
|
|
private overlaysData = [];
|
|
|
private graphicsType = '';
|
|
|
private plot;
|
|
|
+ private vectorLayer;
|
|
|
|
|
|
constructor(options) {
|
|
|
+ this.options = options;
|
|
|
this.map = new Map({
|
|
|
controls: defaults({
|
|
|
zoom: false,
|
|
@@ -66,20 +79,31 @@ export class olMap {
|
|
|
if (options.showScale) {
|
|
|
// map.addControl(new AMap.Scale());
|
|
|
}
|
|
|
- if (options.drawTool.use) {
|
|
|
+ if (options.drawTool?.use) {
|
|
|
this.initMouseTool(options.drawTool);
|
|
|
}
|
|
|
- if (typeof options.onLoadCompleted === 'function') {
|
|
|
- options.onLoadCompleted(this.map);
|
|
|
- }
|
|
|
+ this.initLayer(options);
|
|
|
+ }
|
|
|
+ async initLayer(options) {
|
|
|
// 添加新的图层
|
|
|
if (Array.isArray(options.id)) {
|
|
|
- options.id.forEach((layer)=> {
|
|
|
- this.formatXml(layer);
|
|
|
- });
|
|
|
+ for (const layer of options.id) {
|
|
|
+ await this.formatXml(layer); // 等待当前 layer 处理完成
|
|
|
+ }
|
|
|
} else if (options.id) {
|
|
|
- // 如果newLayers不是数组,但确实是一个图层,则直接添加
|
|
|
- this.formatXml(options.id);
|
|
|
+ // 如果 options.id 不是数组,但确实是一个图层,则直接处理
|
|
|
+ await this.formatXml(options.id);
|
|
|
+ }
|
|
|
+ console.log('wan')
|
|
|
+ // 创建Vector层并添加到地图上
|
|
|
+ this.vectorLayer = new VectorLayer({
|
|
|
+ source: new VectorSource({
|
|
|
+ features: []
|
|
|
+ })
|
|
|
+ });
|
|
|
+ this.map.addLayer(this.vectorLayer);
|
|
|
+ if (typeof this.options.onLoadCompleted === 'function') {
|
|
|
+ this.options.onLoadCompleted(this.map);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -88,7 +112,7 @@ export class olMap {
|
|
|
// axios.post(commonUrl + 'YZT1723547712680', '<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs"service="WFS"version="1.0.0"outputFormat="GeoJson"maxFeatures="99999"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.opengis.net/wfshttp://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd">\n' +
|
|
|
// '<wfs:QuerytypeName="GDSMMSZJGZDTJX_2023"userecent="true"/>\n' +
|
|
|
// '</wfs:GetFeature>');
|
|
|
- this.getCapabilities(code).then((lists) => {
|
|
|
+ return this.getCapabilities(code).then((lists) => {
|
|
|
const mapData = xml.read(lists.data);
|
|
|
const layerParam = {
|
|
|
layerName: mapData.Contents.Layer[0].Title,
|
|
@@ -99,10 +123,12 @@ export class olMap {
|
|
|
this.createWmsLayer(code, layerParam, minZoom, maxZoom, zIndex, visible);
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
// 请求接口获取地图信息
|
|
|
getCapabilities(code) {
|
|
|
return axios.get(commonUrl + code + '?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetCapabilities');
|
|
|
}
|
|
|
+
|
|
|
// 请求地图图片加载图层
|
|
|
createWmsLayer(code, layerParam, minZoom = 0, maxZoom, zIndex = -1, visible = true) {
|
|
|
const source = new WMTS({
|
|
@@ -129,7 +155,9 @@ export class olMap {
|
|
|
layer.set('layerName', code);
|
|
|
|
|
|
this.map.addLayer(layer);
|
|
|
- };
|
|
|
+ console.log(code)
|
|
|
+ }
|
|
|
+
|
|
|
// 初始化绘画工具
|
|
|
initMouseTool(options) {
|
|
|
this.drawOptions = {
|
|
@@ -148,6 +176,7 @@ export class olMap {
|
|
|
// });
|
|
|
// this.plot.plotDraw.on('drawEnd', this.onDrawEnd.bind(this));
|
|
|
}
|
|
|
+
|
|
|
// 绘制结束事件
|
|
|
onDrawEnd(event) {
|
|
|
const feature = event.feature;
|
|
@@ -155,11 +184,12 @@ export class olMap {
|
|
|
// 继续编辑编辑
|
|
|
this.drawGraphics(this.drawOptions.graphicsType);
|
|
|
}
|
|
|
+
|
|
|
// 绘制图形
|
|
|
drawGraphics(type: string) {
|
|
|
if (type === 'circle') {
|
|
|
// 绘制圆形
|
|
|
- activate('Circle');
|
|
|
+ // activate('Circle');
|
|
|
} else if (type === 'rectangle') {
|
|
|
// 绘制矩形
|
|
|
this.plot.plotDraw.activate('RectAngle');
|
|
@@ -171,12 +201,14 @@ export class olMap {
|
|
|
this.plot.plotDraw.activate('FreePolygon');
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
// 关闭绘制
|
|
|
closeDraw() {
|
|
|
this.plot.plotDraw.deactivate();
|
|
|
}
|
|
|
+
|
|
|
// 切换图层
|
|
|
- replaceLayers(newLayers) {
|
|
|
+ async replaceLayers(newLayers) {
|
|
|
// 遍历当前的所有图层并移除它们
|
|
|
this.map.getLayers().forEach((layer) => {
|
|
|
this.map.removeLayer(layer);
|
|
@@ -184,12 +216,63 @@ export class olMap {
|
|
|
|
|
|
// 添加新的图层
|
|
|
if (Array.isArray(newLayers)) {
|
|
|
- newLayers.forEach((layer)=> {
|
|
|
- this.formatXml(layer);
|
|
|
- });
|
|
|
+ for (const layer of newLayers) {
|
|
|
+ await this.formatXml(layer); // 等待当前 layer 处理完成
|
|
|
+ }
|
|
|
} else if (newLayers) {
|
|
|
- // 如果newLayers不是数组,但确实是一个图层,则直接添加
|
|
|
- this.formatXml(newLayers);
|
|
|
+ // 如果 options.id 不是数组,但确实是一个图层,则直接处理
|
|
|
+ await this.formatXml(newLayers);
|
|
|
}
|
|
|
+ // 创建Vector层并添加到地图上
|
|
|
+ this.vectorLayer = new VectorLayer({
|
|
|
+ source: new VectorSource({
|
|
|
+ features: []
|
|
|
+ })
|
|
|
+ });
|
|
|
+ this.map.addLayer(this.vectorLayer);
|
|
|
+ const point = JSON.parse(JSON.stringify(this.markers));
|
|
|
+ this.markers = [];
|
|
|
+ this.addMarker(point)
|
|
|
+ }
|
|
|
+
|
|
|
+ addMarker(points) {
|
|
|
+
|
|
|
+ points.forEach((point) => {
|
|
|
+ // 创建标注点
|
|
|
+ const feature = new Feature({
|
|
|
+ geometry: new Point([point.longitude, point.latitude]),
|
|
|
+ name: point.name
|
|
|
+ });
|
|
|
+
|
|
|
+ // 定义样式
|
|
|
+ const style = new Style({
|
|
|
+ image: new Icon({
|
|
|
+ anchor: [0.5, point.size[1]],
|
|
|
+ anchorXUnits: 'fraction',
|
|
|
+ anchorYUnits: 'pixels',
|
|
|
+ src: point.image
|
|
|
+ }),
|
|
|
+ text: new Text({
|
|
|
+ text: point.name,
|
|
|
+ fill: new Fill({
|
|
|
+ color: '#000'
|
|
|
+ }),
|
|
|
+ stroke: new Stroke({
|
|
|
+ color: '#fff',
|
|
|
+ width: 3
|
|
|
+ })
|
|
|
+ })
|
|
|
+ });
|
|
|
+
|
|
|
+ feature.setStyle(style);
|
|
|
+ this.markers.push(point);
|
|
|
+ this.vectorLayer.getSource().addFeature(feature);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清除所有标加
|
|
|
+ clearMarker(id) {
|
|
|
+ if (!this.vectorLayer) return;
|
|
|
+ this.vectorLayer.getSource().clear();
|
|
|
}
|
|
|
}
|