8000 【fix】修复VectorTileSuperMapRest tileLoadFunction 传入不起作用的问题,修复ol webmap打… · SuperMap/iClient-JavaScript@55f9e4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 55f9e4c

Browse files
committed
【fix】修复VectorTileSuperMapRest tileLoadFunction 传入不起作用的问题,修复ol webmap打开wmts服务比例尺不对的问题 review by qiwei
1 parent d3a55bc commit 55f9e4c

File tree

3 files changed

+66
-30
lines changed

3 files changed

+66
-30
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const dpiConfig = {
9090
* @param {string} [options.proxy] - 代理地址,当域名不一致,请求会加上代理。避免跨域
9191
* @param {string} [options.tileFormat] - 地图瓦片出图格式,png/webp
9292
* @param {function} [options.mapSetting.mapClickCallback] - 地图被点击的回调函数
93-
* @param {function} [options.mapSetting.overlays] - 地图的overlayer
93+
* @param {function} [options.mapSetting.overlays] - 地图的overlayerp
9494
* @param {function} [options.mapSetting.controls] - 地图的控件
9595
* @param {function} [options.mapSetting.interactions] - 地图控制的参数
9696
* @extends {ol/Observable}
@@ -697,8 +697,8 @@ export class WebMap extends Observable {
697697
let visibleScales, minScale, maxScale;
698698
if (baseLayer.layerType === 'WMTS') {
699699
visibleScales = baseLayer.scales;
700-
minScale = mapInfo.minScale.split(':')[1];
701-
maxScale = mapInfo.maxScale.split(':')[1];
700+
minScale = +mapInfo.minScale.split(':')[1];
701+
maxScale = +mapInfo.maxScale.split(':')[1];
702702
} else {
703703
const scales = this.scales.map((scale) => {
704704
return 1 / scale.split(':')[1];
@@ -708,19 +708,16 @@ export class WebMap extends Observable {
708708
} else {
709709
visibleScales = scales;
710710
}
711-
minScale = 1 / mapInfo.minScale.split(':')[1];
712-
maxScale = 1 / mapInfo.maxScale.split(':')[1];
713-
}
714-
if (minScale > maxScale) {
715-
let temp = null;
716-
temp = minScale;
717-
minScale = maxScale;
718-
maxScale = temp;
711+
minScale = 1 / +mapInfo.minScale.split(':')[1];
712+
maxScale = 1 / +mapInfo.maxScale.split(':')[1];
719713
}
720714
const minVisibleScale = this.findNearest(visibleScales, minScale);
721715
const maxVisibleScale = this.findNearest(visibleScales, maxScale);
722-
const minZoom = visibleScales.indexOf(minVisibleScale);
723-
const maxZoom = visibleScales.indexOf(maxVisibleScale);
716+
let minZoom = visibleScales.indexOf(minVisibleScale);
717+
let maxZoom = visibleScales.indexOf(maxVisibleScale);
718+
if (minZoom > maxZoom) {
719+
[minZoom, maxZoom] = [maxZoom, minZoom];
720+
}
724721
if (minZoom !== 0 && maxZoom !== visibleScales.length - 1) {
725722
this.map.setView(
726723
new View(
@@ -1641,6 +1638,10 @@ export class WebMap extends Observable {
16411638
requestEncoding: layerInfo.requestEncoding || 'KVP',
16421639
tileGrid: this.getWMTSTileGrid(extent, layerInfo.scales, unit, layerInfo.dpi, layerInfo.origin, layerInfo.matrixIds),
16431640
tileLoadFunction: function (imageTile, src) {
1641+
if (src.indexOf('tianditu.gov.cn') >= 0) {
1642+
imageTile.getImage().src = `${src}&tk=${CommonUtil.getParameters(layerInfo.url)['tk']}`;
1643+
return;
1644+
}
16441645
imageTile.getImage().src = src
16451646
}
16461647
})

src/openlayers/overlay/VectorTileSuperMapRest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ export class VectorTileSuperMapRest extends VectorTile {
5656
tileClass: options.tileClass,
5757
tileGrid: options.tileGrid,
5858
tilePixelRatio: options.tilePixelRatio,
59-
tileUrlFunction: options.format instanceof MVT && options.style ? zxyTileUrlFunction : tileUrlFunction,
60-
tileLoadFunction: options.format instanceof MVT ? mvtTileLoadFunction : tileLoadFunction,
59+
tileUrlFunction: options.tileUrlFunction || (options.format instanceof MVT && options.style ? zxyTileUrlFunction : tileUrlFunction),
60+
tileLoadFunction: options.tileLoadFunction || (options.format instanceof MVT ? mvtTileLoadFunction : tileLoadFunction),
6161
wrapX: options.wrapX !== undefined ? options.wrapX : false,
6262
tileSize: options.tileSize || null,
6363
zDirection: ['4', '5'].indexOf(Util.getOlVersion()) > -1 ? null : 0

test/openlayers/overlay/VectorTileSuperMapRestSpec.js

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import VectorTileLayer from 'ol/layer/VectorTile';
66

77
var url = GlobeParameter.ChinaURL;
88
describe('openlayers_VectorTileSuperMapRest', () => {
9-
var testDiv, map, vectorTileOptions, vectorTileSource,originalTimeout;
9+
var testDiv, map, vectorTileOptions, vectorTileSource,originalTimeout,vectorLayer;
1010
beforeAll(() => {
1111
testDiv = window.document.createElement("div");
1212
testDiv.setAttribute("id", "map");
@@ -16,6 +16,23 @@ describe('openlayers_VectorTileSuperMapRest', () => {
1616
testDiv.style.width = "500px";
1717
testDiv.style.height = "500px";
1818
window.document.body.appendChild(testDiv);
19+
20+
});
21+
beforeEach(() => {
22+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
23+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
24+
});
25+
afterEach(() => {
26+
if (vectorLayer) {
27+
map.removeLayer(vectorLayer);
28+
}
29+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
30+
});
31+
afterAll(() => {
32+
window.document.body.removeChild(testDiv);
33+
});
34+
35+
it('initialize', (done) => {
1936
new MapService(url).getMapInfo((serviceResult) => {
2037
map = new Map({
2138
target: 'map',
@@ -26,24 +43,11 @@ describe('openlayers_VectorTileSuperMapRest', () => {
2643
});
2744
vectorTileOptions = VectorTileSuperMapRest.optionsFromMapJSON(url, serviceResult.result);
2845
vectorTileSource = new VectorTileSuperMapRest(vectorTileOptions);
29-
var vectorLayer = new VectorTileLayer({
46+
vectorLayer = new VectorTileLayer({
3047
source: vectorTileSource
3148
});
3249
map.addLayer(vectorLayer);
3350
});
34-
});
35-
beforeEach(() => {
36-
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
37-
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
38-
});
39-
afterEach(() => {
40-
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
41-
});
42-
afterAll(() => {
43-
window.document.body.removeChild(testDiv);
44-
});
45-
46-
it('initialize', (done) => {
4751
setTimeout(() => {
4852
try {
4953
expect(vectorTileOptions).not.toBeNull();
@@ -56,4 +60,35 @@ describe('openlayers_VectorTileSuperMapRest', () => {
5660
}
5761
}, 6000);
5862
});
63+
it('custom_tileLoadFunction', (done) => {
64+
var tileLoadFunction = jasmine.createSpy('tileLoadFunction');
65+
new MapService(url).getMapInfo((serviceResult) => {
66+
map = new Map({
67+
target: 'map',
68+
view: new View({
69+
center: [12957388, 4853991],
70+
zoom: 11
71+
})
72+
});
73+
vectorTileOptions = VectorTileSuperMapRest.optionsFromMapJSON(url, serviceResult.result);
74+
vectorTileOptions.tileLoadFunction = tileLoadFunction;
75+
vectorTileSource = new VectorTileSuperMapRest(vectorTileOptions);
76+
vectorLayer = new VectorTileLayer({
77+
source: vectorTileSource
78+
});
79+
map.addLayer(vectorLayer);
80+
});
81+
setTimeout(() => {
82+
try {
83+
expect(vectorTileOptions).not.toBeNull();
84+
expect(vectorTileSource).not.toBeNull();
85+
expect(tileLoadFunction).toHaveBeenCalled();
86+
done();
87+
} catch (exception) {
88+
console.log("'initialize'案例失败:" + exception.name + ":" + exception.message);
89+
expect(false).toBeTruthy();
90+
done();
91+
}
92+
}, 6000);
93+
});
5994
});

0 commit comments

Comments
 (0)
0