Skip to content

Commit 8ef50b7

Browse files
[fix]webmap 重复设置setcrs review by qiw
1 parent 15a7d92 commit 8ef50b7

File tree

6 files changed

+262
-172
lines changed

6 files changed

+262
-172
lines changed

src/common/mapping/WebMapBase.js

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
/* Copyright© 2000 - 2024 SuperMap Software Co.Ltd. All rights reserved.
22
* This program are made available under the terms of the Apache License, Version 2.0
33
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
4-
import { WebMapService } from './WebMapService';
5-
import { transformServerUrl } from './utils/util';
6-
import cloneDeep from 'lodash.clonedeep';
7-
8-
const WORLD_WIDTH = 360;
9-
10-
/**
11-
* @class WebMapBase
12-
* @version 9.1.2
13-
* @category iPortal/Online Resources Map
14-
* @classdesc 对接 iPortal/Online 地图类。
15-
* @modulecategory Mapping
16-
* @param {number} id - iPortal|Online 地图 ID。
17-
* @param {Object} options - 基础参数。
18-
* @param {string} [options.target='map'] - 地图容器 ID。
19-
* @param {string} [options.server="https://www.supermapol.com"] - 地图的地址。
20-
* @param {string} [options.credentialKey] - 凭证密钥。
21-
* @param {string} [options.credentialValue] - 凭证值。
22-
* @param {string} [options.tiandituKey] - 用于访问天地图的服务。当设置 `id` 时有效。
23-
* @param {string} [options.googleMapsAPIKey] - 用于访问谷歌地图。当设置 `id` 时有效。
24-
* @param {string} [options.googleMapsLanguage] - 用于定义在谷歌地图图块上显示标签的语言。当设置 `id` 且底图为谷歌地图时有效。
25-
* @param {boolean} [options.withCredentials=false] - 请求是否携带 cookie。当设置 `id` 时有效。
26-
* @param {boolean} [options.excludePortalProxyUrl] - server 传递过来的 URL 是否带有代理。当设置 `id` 时有效。
27-
* @param {boolean} [options.ignoreBaseProjection = false] - 是否忽略底图坐标系和叠加图层坐标系不一致。
28-
* @param {boolean} [options.isSuperMapOnline] - 是否是 SuperMap Online 地图。
29-
* @param {string} [options.iportalServiceProxyUrlPrefix] - iportal的代理服务地址前缀。
30-
* @param {string|boolean} [options.proxy] - HTTP 请求代理地址 。布尔值表示使用 iPortal 默认代理地址。
31-
* @param {Object} mapOptions - 地图参数。
32-
* @param {Array} [mapOptions.center] - 中心点。
33-
* @param {number} [mapOptions.zoom] - 缩放级别。
34-
* @param {number} [mapOptions.bearing] - 旋转角度。
35-
* @param {number} [mapOptions.pitch] - 倾角。
36-
* @param {string|Object} [mapOptions.crs] - 投影。
37-
* @param {boolean} [mapOptions.renderWorldCopies] - 连续渲染。
38-
* @param {number} [mapOptions.rasterTileSize] - 栅格瓦片大小。
39-
* @param {Object} [mapOptions.style] - style 样式。
40-
* @fires WebMapBase#mapinitialized
41-
* @fires WebMapBase#mapcreatesucceeded
42-
* @fires WebMapBase#mapcreatefailed
43-
* @fires WebMapBase#addlayerssucceeded
44-
* @fires WebMapBase#layercreatefailed
45-
* @fires WebMapBase#baidumapnotsupport
46-
* @fires WebMapBase#dataflowfeatureupdated
47-
* @fires WebMapBase#projectionnotmatch
48-
* @fires WebMapBase#mapbeforeremove
49-
* @fires WebMapBase#getmapfailed
50-
* @fires WebMapBase#getlayersfailed
51-
* @usage
52-
*/
53-
export function createWebMapBaseExtending(SuperClass, { mapRepo }) {
4+
import { WebMapService } from './WebMapService';
5+
import { transformServerUrl } from './utils/util';
6+
import cloneDeep from 'lodash.clonedeep';
7+
8+
const WORLD_WIDTH = 360;
9+
10+
/**
11+
* @class WebMapBase
12+
* @version 9.1.2
13+
* @category iPortal/Online Resources Map
14+
* @classdesc 对接 iPortal/Online 地图类。
15+
* @modulecategory Mapping
16+
* @param {number} id - iPortal|Online 地图 ID。
17+
* @param {Object} options - 基础参数。
18+
* @param {string} [options.target='map'] - 地图容器 ID。
19+
* @param {string} [options.server="https://www.supermapol.com"] - 地图的地址。
20+
* @param {string} [options.credentialKey] - 凭证密钥。
21+
* @param {string} [options.credentialValue] - 凭证值。
22+
* @param {string} [options.tiandituKey] - 用于访问天地图的服务。当设置 `id` 时有效。
23+
* @param {string} [options.googleMapsAPIKey] - 用于访问谷歌地图。当设置 `id` 时有效。
24+
* @param {string} [options.googleMapsLanguage] - 用于定义在谷歌地图图块上显示标签的语言。当设置 `id` 且底图为谷歌地图时有效。
25+
* @param {boolean} [options.withCredentials=false] - 请求是否携带 cookie。当设置 `id` 时有效。
26+
* @param {boolean} [options.excludePortalProxyUrl] - server 传递过来的 URL 是否带有代理。当设置 `id` 时有效。
27+
* @param {boolean} [options.ignoreBaseProjection = false] - 是否忽略底图坐标系和叠加图层坐标系不一致。
28+
* @param {boolean} [options.isSuperMapOnline] - 是否是 SuperMap Online 地图。
29+
* @param {string} [options.iportalServiceProxyUrlPrefix] - iportal的代理服务地址前缀。
30+
* @param {string|boolean} [options.proxy] - HTTP 请求代理地址 。布尔值表示使用 iPortal 默认代理地址。
31+
* @param {Object} mapOptions - 地图参数。
32+
* @param {Array} [mapOptions.center] - 中心点。
33+
* @param {number} [mapOptions.zoom] - 缩放级别。
34+
* @param {number} [mapOptions.bearing] - 旋转角度。
35+
* @param {number} [mapOptions.pitch] - 倾角。
36+
* @param {string|Object} [mapOptions.crs] - 投影。
37+
* @param {boolean} [mapOptions.renderWorldCopies] - 连续渲染。
38+
* @param {number} [mapOptions.rasterTileSize] - 栅格瓦片大小。
39+
* @param {Object} [mapOptions.style] - style 样式。
40+
* @fires WebMapBase#mapinitialized
41+
* @fires WebMapBase#mapcreatesucceeded
42+
* @fires WebMapBase#mapcreatefailed
43+
* @fires WebMapBase#addlayerssucceeded
44+
* @fires WebMapBase#layercreatefailed
45+
* @fires WebMapBase#baidumapnotsupport
46+
* @fires WebMapBase#dataflowfeatureupdated
47+
* @fires WebMapBase#projectionnotmatch
48+
* @fires WebMapBase#mapbeforeremove
49+
* @fires WebMapBase#getmapfailed
50+
* @fires WebMapBase#getlayersfailed
51+
* @usage
52+
*/
53+
export function createWebMapBaseExtending(SuperClass, { mapRepo }) {
5454
return class WebMapBase extends SuperClass {
5555
constructor(
5656
id,
@@ -196,6 +196,9 @@
196196
if (this.map) {
197197
this.mapOptions.crs = crs;
198198
if (this.mapOptions.crs) {
199+
if (this.map.getCRS(typeof crs === 'string' ? crs : crs.epsgCode)) {
200+
return;
201+
}
199202
if (crs.epsgCode) {
200203
this.mapOptions.crs = new mapRepo.CRS(
201204
this.mapOptions.crs.epsgCode,
@@ -343,7 +346,7 @@
343346
this.setServerUrl(webMapOptions.server);
344347
this._createWebMap();
345348
}
346-
349+
347350
/**
348351
* @deprecated
349352
* @function WebMapBase.prototype.setMapOptions
@@ -416,7 +419,7 @@
416419
getWebMapType() {
417420
return this.type;
418421
}
419-
422+
420423
/**
421424
* @version 11.2.1
422425
* @function WebMapBase.prototype.rectifyLayersOrder
@@ -486,7 +489,7 @@
486489
return;
487490
}
488491
const sourceList = [];
489-
const layersToClean = this._cacheCleanLayers.filter(item => !item.reused);
492+
const layersToClean = this._cacheCleanLayers.filter((item) => !item.reused);
490493
for (const item of layersToClean) {
491494
item.renderLayers.forEach((layerId) => {
492495
if (this.map.getLayer(layerId)) {
@@ -726,4 +729,4 @@
726729
});
727730
}
728731
};
729-
}
732+
}

src/common/mapping/WebMapV2.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo }) {
9797
}
9898

9999
_setCRS(baseProjection, wkt, bounds) {
100+
if (mapRepo.CRS.get(baseProjection)) {
101+
return;
102+
}
100103
const crs = new mapRepo.CRS(baseProjection, wkt, bounds, bounds[2] > 180 ? 'meter' : 'degree');
101104
mapRepo.CRS.set(crs);
102105
}
@@ -2484,7 +2487,7 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo }) {
24842487
default:
24852488
break;
24862489
}
2487-
const matchIndex = this._legendList.findIndex(item => item.layerId === layerInfo.layerID);
2490+
const matchIndex = this._legendList.findIndex((item) => item.layerId === layerInfo.layerID);
24882491
if (matchIndex > -1) {
24892492
this._legendList.splice(matchIndex, 1, legendItem);
24902493
return;
@@ -2984,7 +2987,7 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo }) {
29842987
}
29852988

29862989
_getVisibility(visible) {
2987-
const visibility = (visible === true || visible === 'visible') ? 'visible' : 'none';
2990+
const visibility = visible === true || visible === 'visible' ? 'visible' : 'none';
29882991
return visibility;
29892992
}
29902993
};

src/common/mapping/WebMapV3.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, mapRe
340340
}
341341

342342
_setCRS({ name, wkt, extent }) {
343+
if (mapRepo.CRS.get(name)) {
344+
return;
345+
}
343346
const crs = new mapRepo.CRS(name, wkt, extent, extent[2] > 180 ? 'meter' : 'degree');
344347
mapRepo.CRS.set(crs);
345348
}

test/mapboxgl/mapping/WebMapSpec.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ describe('mapboxgl_WebMap', () => {
10341034
});
10351035
});
10361036

1037-
it('switch map and reset center zoom', (done) => {
1037+
it('switch map and reset center zoom CRS', (done) => {
10381038
spyOn(FetchRequest, 'get').and.callFake((url) => {
10391039
if (url.indexOf('web/config/portal.json') > -1) {
10401040
return Promise.resolve(new Response(JSON.stringify(iportal_serviceProxy)));
@@ -1092,7 +1092,7 @@ describe('mapboxgl_WebMap', () => {
10921092
datavizWebmap.setStyle({});
10931093
expect(datavizWebmap.mapOptions.zoom).toBeNull();
10941094
expect(datavizWebmap.mapOptions.center).toBeNull();
1095-
datavizWebmap.once('mapcreatesucceeded', () => {
1095+
datavizWebmap.once('mapcreatesucceeded', ({ map }) => {
10961096
center = [116, 30];
10971097
zoom = 16;
10981098
datavizWebmap.setCenter(center);
@@ -1121,6 +1121,19 @@ describe('mapboxgl_WebMap', () => {
11211121
});
11221122
expect(datavizWebmap.mapOptions.zoom).toBe(zoom);
11231123
expect(datavizWebmap.mapOptions.center).toEqual(center);
1124+
datavizWebmap.map = map;
1125+
spyOn( map, 'setCRS');
1126+
spyOn(map, 'getCRS').and.callFake((crs) => {
1127+
if (crs === 'EPSG:4326') {
1128+
return crs;
1129+
}
1130+
return null;
1131+
});
1132+
datavizWebmap.setCRS('EPSG:4326');
1133+
expect(map.setCRS).not.toHaveBeenCalled();
1134+
datavizWebmap.setCRS('EPSG:3857');
1135+
expect(map.setCRS).toHaveBeenCalled();
1136+
datavizWebmap.map = null;
11241137
datavizWebmap.once('mapcreatesucceeded', ({ layers }) => {
11251138
expect(layers.length).toBe(1);
11261139
done();
@@ -1241,11 +1254,7 @@ describe('mapboxgl_WebMap', () => {
12411254
target: 'map',
12421255
withCredentials: false
12431256
};
1244-
datavizWebmap = new WebMap(
1245-
'',
1246-
{ ...commonOption },
1247-
mapOptionsList[0]
1248-
);
1257+
datavizWebmap = new WebMap('', { ...commonOption }, mapOptionsList[0]);
12491258
const callback = function () {
12501259
let layers = datavizWebmap.getLayers();
12511260
expect(layers.length).toBe(2);
@@ -1270,18 +1279,14 @@ describe('mapboxgl_WebMap', () => {
12701279
target: 'map',
12711280
withCredentials: false
12721281
};
1273-
datavizWebmap = new WebMap(
1274-
'',
1275-
{ ...commonOption },
1276-
mapOptionsList[0]
1277-
);
1278-
const callback = function ({map}) {
1282+
datavizWebmap = new WebMap('', { ...commonOption }, mapOptionsList[0]);
1283+
const callback = function ({ map }) {
12791284
let layers = datavizWebmap.getLayers();
12801285
expect(layers.length).toBe(2);
12811286
let newLayers = [layers[1], layers[0]];
12821287
datavizWebmap.rectifyLayersOrder(newLayers);
12831288
const layersOnMap = map.getStyle().layers;
1284-
expect(layersOnMap[0].id).toBe('未命名数据')
1289+
expect(layersOnMap[0].id).toBe('未命名数据');
12851290
done();
12861291
};
12871292
datavizWebmap.once('mapcreatesucceeded', callback);
@@ -1306,11 +1311,7 @@ describe('mapboxgl_WebMap', () => {
13061311
target: 'map',
13071312
withCredentials: false
13081313
};
1309-
var datavizWebmap1 = new WebMap(
1310-
'',
1311-
{ ...commonOption },
1312-
mapOptionsList[0]
1313-
);
1314+
var datavizWebmap1 = new WebMap('', { ...commonOption }, mapOptionsList[0]);
13141315

13151316
const callback = function () {
13161317
var datavizWebmap2 = new WebMap(id, {

0 commit comments

Comments
 (0)