|
@@ -89,7 +89,7 @@ export class olMap {
|
|
|
});
|
|
|
// 初始化比例尺
|
|
|
if (options.showScale) {
|
|
|
- // map.addControl(new AMap.Scale());
|
|
|
+ // this.map.addControl(new AMap.Scale());
|
|
|
}
|
|
|
if (options.drawTool?.use) {
|
|
|
this.initMouseTool(options.drawTool);
|
|
@@ -100,11 +100,15 @@ export class olMap {
|
|
|
// 添加新的图层
|
|
|
if (Array.isArray(options.id)) {
|
|
|
for (const layer of options.id) {
|
|
|
- await this.formatXml(layer); // 等待当前 layer 处理完成
|
|
|
+ if (typeof layer === "string") {
|
|
|
+ await this.formatXml(layer); // 等待当前 layer 处理完成
|
|
|
+ } else {
|
|
|
+ await this.formatXml(layer.id, layer.minZoom, layer.maxZoom, layer.zIndex, layer.visible); // 等待当前 layer 处理完成
|
|
|
+ }
|
|
|
}
|
|
|
} else if (options.id) {
|
|
|
// 如果 options.id 不是数组,但确实是一个图层,则直接处理
|
|
|
- await this.formatXml(options.id);
|
|
|
+ await this.formatXml(options.id, options.minZoom, options.maxZoom, options.zIndex, options.visible);
|
|
|
}
|
|
|
// 创建Vector层并添加到地图上
|
|
|
this.vectorLayer = new VectorLayer({
|
|
@@ -118,11 +122,8 @@ export class olMap {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- formatXml(code, minZoom, maxZoom, zIndex, visible) {
|
|
|
+ formatXml(code: string, minZoom?: number, maxZoom?: number, zIndex?: number, visible?: boolean) {
|
|
|
const xml = new WMTSCapabilities();
|
|
|
- // 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>');
|
|
|
return this.getCapabilities(code).then((lists) => {
|
|
|
const geojson = xml.read(lists.data);
|
|
|
const data = geojson.Contents.Layer[0];
|
|
@@ -158,6 +159,7 @@ export class olMap {
|
|
|
})
|
|
|
});
|
|
|
const layer = new TileLayer({
|
|
|
+ name: code,
|
|
|
source: source,
|
|
|
zIndex: zIndex,
|
|
|
minZoom: minZoom,
|
|
@@ -167,7 +169,6 @@ export class olMap {
|
|
|
layer.set('layerName', code);
|
|
|
|
|
|
this.map.addLayer(layer);
|
|
|
- console.log(code);
|
|
|
}
|
|
|
|
|
|
// 初始化绘画工具
|
|
@@ -219,20 +220,23 @@ export class olMap {
|
|
|
}
|
|
|
|
|
|
// 切换图层
|
|
|
- async replaceLayers(newLayers) {
|
|
|
+ async replaceLayers(newLayers, loadendFunc) {
|
|
|
// 遍历当前的所有图层并移除它们
|
|
|
this.map.getLayers().forEach((layer) => {
|
|
|
this.map.removeLayer(layer);
|
|
|
});
|
|
|
|
|
|
- // 添加新的图层
|
|
|
if (Array.isArray(newLayers)) {
|
|
|
for (const layer of newLayers) {
|
|
|
- await this.formatXml(layer); // 等待当前 layer 处理完成
|
|
|
+ if (typeof layer === "string") {
|
|
|
+ await this.formatXml(layer); // 等待当前 layer 处理完成
|
|
|
+ } else {
|
|
|
+ await this.formatXml(layer.id, layer.minZoom, layer.maxZoom, layer.zIndex, layer.visible); // 等待当前 layer 处理完成
|
|
|
+ }
|
|
|
}
|
|
|
- } else if (newLayers) {
|
|
|
+ } else {
|
|
|
// 如果 options.id 不是数组,但确实是一个图层,则直接处理
|
|
|
- await this.formatXml(newLayers);
|
|
|
+ await this.formatXml(newLayers.id, newLayers.minZoom, newLayers.maxZoom, newLayers.zIndex, newLayers.visible);
|
|
|
}
|
|
|
// 创建Vector层并添加到地图上
|
|
|
this.vectorLayer = new VectorLayer({
|
|
@@ -244,6 +248,9 @@ export class olMap {
|
|
|
const point = JSON.parse(JSON.stringify(this.markers));
|
|
|
this.markers = [];
|
|
|
this.addMarker(point);
|
|
|
+ if (loadendFunc) {
|
|
|
+ loadendFunc();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
addMarker(points) {
|
|
@@ -379,16 +386,12 @@ export class olMap {
|
|
|
removeMask() {
|
|
|
//移除图层
|
|
|
const layersArray = this.map.getLayers().getArray();
|
|
|
- const layers = [];
|
|
|
layersArray.forEach((layer) => {
|
|
|
// 检查图层是否有自定义属性,并且该属性是否匹配你要移除的图层的标识符
|
|
|
if (layer.get('name') === 'mask') {
|
|
|
- layers.push(layer);
|
|
|
+ this.map.removeLayer(layer);
|
|
|
}
|
|
|
});
|
|
|
- layers.forEach((layer) => {
|
|
|
- this.map.removeLayer(layer);
|
|
|
- });
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -397,43 +400,50 @@ export class olMap {
|
|
|
*/
|
|
|
handleLngLatLine(visible: boolean) {
|
|
|
// 创建经纬网图层
|
|
|
- let graticule = new Graticule({
|
|
|
+ const graticule = new Graticule({
|
|
|
+ name: 'Graticule',
|
|
|
showLabels: true, // 为每条刻度线绘制一个带有各自纬度/经度的标签
|
|
|
- wrapX: true, // 是否水平重复经纬网
|
|
|
+ wrapX: false, // 是否水平重复经纬网
|
|
|
targetSize: 230,
|
|
|
zIndex: 99999,
|
|
|
strokeStyle: new Stroke({ // 用于绘制刻度线的样式
|
|
|
color: '#dcdcdc', // 线条颜色
|
|
|
width: 1, // 线条宽度
|
|
|
}),
|
|
|
- // lonLabelFormatter: (data) => {
|
|
|
- // return data;
|
|
|
- // },
|
|
|
- // latLabelFormatter: (data) => {
|
|
|
- // return data;
|
|
|
- // },
|
|
|
- lonLabelPosition: 'top',
|
|
|
- // latLabelPosition: 'left',
|
|
|
- latLabelStyle: new Text({
|
|
|
+ lonLabelStyle: new Text({
|
|
|
font: '12px Calibri,sans-serif',
|
|
|
textBaseline: 'bottom',
|
|
|
fill: new Fill({
|
|
|
color: '#9d9d9d'
|
|
|
})
|
|
|
+ }),
|
|
|
+ latLabelPosition: 0,
|
|
|
+ latLabelStyle: new Text({
|
|
|
+ font: '12px Calibri,sans-serif',
|
|
|
+ textAlign: 'left',
|
|
|
+ textBaseline: 'end',
|
|
|
+ fill: new Fill({
|
|
|
+ color: '#9d9d9d'
|
|
|
+ })
|
|
|
})
|
|
|
});
|
|
|
if (visible) {
|
|
|
this.map.addLayer(graticule)
|
|
|
} else {
|
|
|
- var layers = this.map.getLayers();
|
|
|
- layers.forEach(element => {
|
|
|
- //假设每个层都有一个名为 'name' 的属性
|
|
|
- console.log(element.get('name'));
|
|
|
- if (element.get('name')=='Graticule') {
|
|
|
- //移除
|
|
|
- this.map.removeLayer(element)
|
|
|
- }
|
|
|
- });
|
|
|
+ this.removeLayer('Graticule')
|
|
|
}
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 移除指定name的layer
|
|
|
+ * layerName: string
|
|
|
+ * */
|
|
|
+ removeLayer(layerName: string) {
|
|
|
+ const layers = this.map.getLayers();
|
|
|
+ layers.forEach((element) => {
|
|
|
+ if (!!element && element.get('name') === layerName) {
|
|
|
+ //移除
|
|
|
+ this.map.removeLayer(element)
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|