Skip to content

Commit d4be1d8

Browse files
【fix】 修复ol webmap 新增固定比例尺后报错 review by songym
1 parent a718c78 commit d4be1d8

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import MVT from 'ol/format/MVT';
2929
import Observable from 'ol/Observable';
3030
import olMap from 'ol/Map';
3131
import View from 'ol/View';
32+
import MouseWheelZoom from 'ol/interaction/MouseWheelZoom';
3233
import * as olProj from 'ol/proj';
3334
import * as olProj4 from 'ol/proj/proj4';
3435
import Units from 'ol/proj/Units';
@@ -673,7 +674,83 @@ export class WebMap extends Observable {
673674
// 挂载带baseLayer上,便于删除
674675
baseLayer.labelLayer = labelLayer;
675676
}
676-
}
677+
this.limitScale(mapInfo, baseLayer);
678+
}
679+
680+
validScale(scale) {
681+
if (!scale) {
682+
return false;
683+
}
684+
const scaleNum = scale.split(':')[1];
685+
if (!scaleNum) {
686+
return false;
687+
}
688+
const res = 1 / +scaleNum;
689+
if (res === Infinity || res >= 1) {
690+
return false;
691+
}
692+
return true;
693+
}
694+
695+
limitScale(mapInfo, baseLayer) {
696+
if (this.validScale(mapInfo.minScale) && this.validScale(mapInfo.maxScale)) {
697+
let visibleScales, minScale, maxScale;
698+
if (baseLayer.layerType === 'WMTS') {
699+
visibleScales = baseLayer.scales;
700+
minScale = mapInfo.minScale.split(':')[1];
701+
maxScale = mapInfo.maxScale.split(':')[1];
702+
} else {
703+
const scales = this.scales.map((scale) => {
704+
return 1 / scale.split(':')[1];
705+
});
706+
visibleScales = baseLayer.visibleScales || scales;
707+
minScale = 1 / mapInfo.minScale.split(':')[1];
708+
maxScale = 1 / mapInfo.maxScale.split(':')[1];
709+
}
710+
711+
const minVisibleScale = this.findNearest(visibleScales, minScale);
712+
const maxVisibleScale = this.findNearest(visibleScales, maxScale);
713+
const minZoom = visibleScales.indexOf(minVisibleScale);
714+
const maxZoom = visibleScales.indexOf(maxVisibleScale);
715+
if (minZoom !== 0 && maxZoom !== visibleScales.length - 1) {
716+
this.map.setView(
717+
new View(
718+
Object.assign({}, this.map.getView().options_, {
719+
maxResolution: undefined,
720+
minResolution: undefined,
721+
minZoom,
722+
maxZoom,
723+
constrainResolution: false
724+
})
725+
)
726+
);
727+
this.map.addInteraction(
728+
new MouseWheelZoom({
729+
constrainResolution: true
730+
})
731+
);
732+
}
733+
}
734+
}
735+
736+
parseNumber(scaleStr) {
737+
return Number(scaleStr.split(":")[1])
738+
}
739+
740+
findNearest(scales, target) {
741+
let resultIndex = 0
742+
let targetScaleD = target;
743+
for (let i = 1, len = scales.length; i < len; i++) {
744+
if (
745+
Math.abs(scales[i] - targetScaleD) <
746+
Math.abs(scales[resultIndex] - targetScaleD)
747+
) {
748+
resultIndex = i
749+
}
750+
}
751+
return scales[resultIndex]
752+
}
753+
677754
/**
678755
* @private
679756
* @function ol.supermap.WebMap.prototype.addMVTMapLayer
@@ -752,6 +829,7 @@ export class WebMap extends Observable {
752829
// if(options.baseLayer.visibleScales && options.baseLayer.visibleScales.length > 0){
753830
// maxZoom = options.baseLayer.visibleScales.length;
754831
// }
832+
this.map.setView(new View({ zoom, center, projection, maxZoom }));
755833
let viewOptions = {};
756834

757835
if (baseLayer.scales && baseLayer.scales.length > 0 && baseLayer.layerType === "WMTS" ||

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy