Skip to content

Commit 1c1b380

Browse files
committed
【update】1) webMap增加参数webmap. 为了解决dv接入ipt注册服务进来的地图,通过分享页面打开。
2)增加单元测试 (reviewed by songym)
1 parent 5644732 commit 1c1b380

File tree

3 files changed

+149
-67
lines changed

3 files changed

+149
-67
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 101 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ const dpiConfig = {
7171
* @class ol.supermap.WebMap
7272
* @category iPortal/Online
7373
* @classdesc 对接 iPortal/Online 地图类
74-
* @param {number} id - 地图的id
7574
* @param {Object} options - 参数
7675
* @param {string} [options.target='map'] - 地图容器id
77-
* @param {string} [options.server="https://www.supermapol.com"] - 地图的地址
76+
* @param {Object | string} [options.webMap] - webMap对象,或者是获取webMap的url地址。存在webMap,优先使用webMap, id的选项则会被忽略
77+
* @param {number} [options.id] - 地图的id
78+
* @param {string} [options.server="https://www.supermapol.com"] - 地图的地址,如果使用传入id,server则会和id拼接成webMap请求地址
7879
* @param {function} [options.successCallback] - 成功加载地图后调用的函数
7980
* @param {function} [options.errorCallback] - 加载地图失败调用的函数
8081
* @param {string} [options.credentialKey] - 凭证密钥。例如为"key"、"token",或者用户自定义的密钥。用户申请了密钥,此参数必填
@@ -94,9 +95,14 @@ export class WebMap extends Observable {
9495

9596
constructor(id, options) {
9697
super();
97-
this.mapId = id;
98+
if(Util.isObject(id)) {
99+
options = id;
100+
this.mapId = options.id;
101+
} else {
102+
this.mapId = id;
103+
}
98104
options = options || {};
99-
this.server = options.server || 'https://www.supermapol.com';
105+
this.server = options.server;
100106
this.successCallback = options.successCallback;
101107
this.errorCallback = options.errorCallback;
102108
this.credentialKey = options.credentialKey;
@@ -111,8 +117,17 @@ export class WebMap extends Observable {
111117
this.layerAdded = 0;
112118
this.layers = [];
113119
this.events = new Events(this, null, ["updateDataflowFeature"], true);
120+
this.webMap = options.webMap;
114121
this.createMap(options.mapSetting);
115-
this.createWebmap();
122+
if(this.webMap) {
123+
// webmap有可能是url地址,有可能是webmap对象
124+
Util.isString(this.webMap) ? this.createWebmap(this.webMap) : this.getMapInfoSuccess(options.webMap);
125+
} else {
126+
if(!this.server) {
127+
this.server = 'https://www.supermapol.com';
128+
}
129+
this.createWebmap();
130+
}
116131
}
117132

118133
/**
@@ -227,20 +242,25 @@ export class WebMap extends Observable {
227242
* @private
228243
* @function ol.supermap.WebMap.prototype.createWebmap
229244
* @description 创建webmap
245+
* @param {string} webMapUrl - 请求webMap的地址
230246
*/
231-
createWebmap() {
232-
let urlArr = this.server.split('');
233-
if (urlArr[urlArr.length - 1] !== '/') {
234-
this.server += '/';
235-
}
236-
237-
let mapUrl = this.server + 'web/maps/' + this.mapId + '/map';
238-
let filter = 'getUrlResource.json?url=';
239-
if (this.excludePortalProxyUrl && this.server.indexOf(filter) > -1) {
240-
//大屏需求,或者有加上代理的
241-
let urlArray = this.server.split(filter);
242-
if (urlArray.length > 1) {
243-
mapUrl = urlArray[0] + filter + this.server + 'web/maps/' + this.mapId + '/map.json';
247+
createWebmap(webMapUrl) {
248+
let mapUrl;
249+
if(webMapUrl) {
250+
mapUrl = webMapUrl;
251+
} else {
252+
let urlArr = this.server.split('');
253+
if (urlArr[urlArr.length - 1] !== '/') {
254+
this.server += '/';
255+
}
256+
mapUrl = this.server + 'web/maps/' + this.mapId + '/map';
257+
let filter = 'getUrlResource.json?url=';
258+
if (this.excludePortalProxyUrl && this.server.indexOf(filter) > -1) {
259+
//大屏需求,或者有加上代理的
260+
let urlArray = this.server.split(filter);
261+
if (urlArray.length > 1) {
262+
mapUrl = urlArray[0] + filter + this.server + 'web/maps/' + this.mapId + '/map.json';
263+
}
244264
}
245265
}
246266
this.getMapInfo(mapUrl);
@@ -263,60 +283,75 @@ export class WebMap extends Observable {
263283
withCredentials: this.withCredentials
264284
}).then(function (response) {
265285
return response.json();
266-
}).then(async function (mapInfo) {
267-
if (mapInfo.succeed === false) {
268-
that.errorCallback && that.errorCallback(mapInfo.error, 'getMapFaild', that.map);
269-
return;
270-
}
271-
if(mapInfo.projection === 'EPSG:910111' || mapInfo.projection === 'EPSG:910112'){
272-
// 早期数据存在的自定义坐标系 "EPSG:910111": "GCJ02MERCATOR", "EPSG:910112": "BDMERCATOR"
273-
mapInfo.projection = "EPSG:3857";
274-
}else if(mapInfo.projection === 'EPSG:910101' || mapInfo.projection === 'EPSG:910102'){
275-
// 早期数据存在的自定义坐标系 "EPSG:910101": "GCJ02", "EPSG:910102": "BD",
276-
mapInfo.projection = "EPSG:4326";
277-
}
278-
that.baseProjection = mapInfo.projection;
279-
that.webMapVersion = mapInfo.version;
280-
that.baseLayer = mapInfo.baseLayer;
281-
that.mapParams = {
282-
title: mapInfo.title,
283-
description: mapInfo.description
284-
}; //存储地图的名称以及描述等信息,返回给用户
285-
286-
// 目前iServer服务中可能出现的EPSG 0,-1,-1000
287-
if(mapInfo.projection.indexOf("EPSG") === 0 && mapInfo.projection.split(":")[1] <= 0){
288-
//对于这两种地图,只能view,不能叠加其他图层
289-
that.createSpecLayer(mapInfo);
290-
return;
291-
} else if(that.addProjctionFromWKT(mapInfo.projection)){
292-
mapInfo.projection = that.baseProjection = that.getEpsgInfoFromWKT(mapInfo.projection);
293-
}else{
294-
// 不支持的坐标系
295-
that.errorCallback && that.errorCallback({type: "Not support CS", errorMsg: `Not support CS: ${mapInfo.projection}`}, 'getMapFaild', that.map);
296-
return;
297-
}
286+
}).then(function (mapInfo) {
287+
that.getMapInfoSuccess(mapInfo);
288+
}).catch(function (error) {
289+
that.errorCallback && that.errorCallback(error, 'getMapFaild', that.map);
290+
});
291+
}
298292

299-
if (mapInfo.baseLayer && mapInfo.baseLayer.layerType === 'MAPBOXSTYLE') {
300-
// 添加矢量瓦片服务作为底图
301-
that.addMVTMapLayer(mapInfo, mapInfo.baseLayer, 0).then(() => {
302-
that.createView(mapInfo);
303-
if (!mapInfo.layers || mapInfo.layers.length === 0) {
304-
that.sendMapToUser(0);
305-
} else {
306-
that.addLayers(mapInfo);
307-
}
308-
});
309-
} else {
310-
await that.addBaseMap(mapInfo);
293+
/**
294+
* @private
295+
* @function ol.supermap.WebMap.prototype.getMapInfoSuccess
296+
* @description 获取地图的json信息
297+
* @param {Object} mapInfo - webMap对象
298+
*/
299+
async getMapInfoSuccess(mapInfo) {
300+
let that = this;
301+
if (mapInfo.succeed === false) {
302+
that.errorCallback && that.errorCallback(mapInfo.error, 'getMapFaild', that.map);
303+
return;
304+
}
305+
if(mapInfo.projection === 'EPSG:910111' || mapInfo.projection === 'EPSG:910112'){
306+
// 早期数据存在的自定义坐标系 "EPSG:910111": "GCJ02MERCATOR", "EPSG:910112": "BDMERCATOR"
307+
mapInfo.projection = "EPSG:3857";
308+
}else if(mapInfo.projection === 'EPSG:910101' || mapInfo.projection === 'EPSG:910102'){
309+
// 早期数据存在的自定义坐标系 "EPSG:910101": "GCJ02", "EPSG:910102": "BD",
310+
mapInfo.projection = "EPSG:4326";
311+
}
312+
// 传入webMap的就使用rootUrl.没有传入就用默认值
313+
if(that.webMap) {
314+
that.server = mapInfo.rootUrl;
315+
}
316+
that.baseProjection = mapInfo.projection;
317+
that.webMapVersion = mapInfo.version;
318+
that.baseLayer = mapInfo.baseLayer;
319+
that.mapParams = {
320+
title: mapInfo.title,
321+
description: mapInfo.description
322+
}; //存储地图的名称以及描述等信息,返回给用户
323+
324+
// 目前iServer服务中可能出现的EPSG 0,-1,-1000
325+
if(mapInfo.projection.indexOf("EPSG") === 0 && mapInfo.projection.split(":")[1] <= 0){
326+
//对于这两种地图,只能view,不能叠加其他图层
327+
that.createSpecLayer(mapInfo);
328+
return;
329+
} else if(that.addProjctionFromWKT(mapInfo.projection)){
330+
mapInfo.projection = that.baseProjection = that.getEpsgInfoFromWKT(mapInfo.projection);
331+
}else{
332+
// 不支持的坐标系
333+
that.errorCallback && that.errorCallback({type: "Not support CS", errorMsg: `Not support CS: ${mapInfo.projection}`}, 'getMapFaild', that.map);
334+
return;
335+
}
336+
337+
if (mapInfo.baseLayer && mapInfo.baseLayer.layerType === 'MAPBOXSTYLE') {
338+
// 添加矢量瓦片服务作为底图
339+
that.addMVTMapLayer(mapInfo, mapInfo.baseLayer, 0).then(() => {
340+
that.createView(mapInfo);
311341
if (!mapInfo.layers || mapInfo.layers.length === 0) {
312342
that.sendMapToUser(0);
313343
} else {
314344
that.addLayers(mapInfo);
315345
}
346+
});
347+
} else {
348+
await that.addBaseMap(mapInfo);
349+
if (!mapInfo.layers || mapInfo.layers.length === 0) {
350+
that.sendMapToUser(0);
351+
} else {
352+
that.addLayers(mapInfo);
316353
}
317-
}).catch(function (error) {
318-
that.errorCallback && that.errorCallback(error, 'getMapFaild', that.map);
319-
});
354+
}
320355
}
321356
/**
322357
* @private
@@ -1675,7 +1710,7 @@ export class WebMap extends Observable {
16751710
let geojson = that.excelData2FeatureByDivision(data.content, divisionType, divisionField);
16761711
features = that._parseGeoJsonData2Feature({ allDatas: { features: geojson.features }, fileCode: layerInfo.projection });
16771712
} else {
1678-
features = that.excelData2Feature(data.content, layerInfo);
1713+
features = await that.excelData2Feature(data.content, layerInfo);
16791714
}
16801715
} else {
16811716
var geoJson = data.content ? JSON.parse(data.content) : data;

test/openlayers/mapping/WebMapSpec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,5 +1004,52 @@ describe('openlayers_WebMap', () => {
10041004
}, 1000);
10051005
}, 1000)
10061006
})
1007+
it('webMapUrl', (done) => {
1008+
let options = {
1009+
server: server,
1010+
webMap: defaultServeRequest,
1011+
successCallback: function () {},
1012+
errorCallback: function () {}
1013+
};
1014+
spyOn(FetchRequest, 'get').and.callFake((url) => {
1015+
if (url.indexOf('map.json') > -1) {
1016+
var mapJson = datavizWebMap_BAIDU;
1017+
return Promise.resolve(new Response(mapJson));
1018+
}
1019+
return Promise.resolve();
1020+
});
1021+
spyOn(options, 'successCallback');
1022+
var datavizWebmap = new WebMap(options);
1023+
1024+
setTimeout(() => {
1025+
expect(datavizWebmap.server).toBe(server);
1026+
expect(datavizWebmap.errorCallback).toBeDefined();
1027+
expect(datavizWebmap.credentialKey).toBeUndefined();
1028+
expect(datavizWebmap.credentialValue).toBeUndefined();
10071029

1030+
expect(datavizWebmap.mapParams.title).toBe('百度');
1031+
expect(datavizWebmap.mapParams.description).toBe('');
1032+
expect(options.successCallback).toHaveBeenCalled();
1033+
done();
1034+
}, 1000)
1035+
})
1036+
it('webMapObject', (done) => {
1037+
let options = {
1038+
server: server,
1039+
webMap: JSON.parse(datavizWebMap_BAIDU),
1040+
successCallback: function () {},
1041+
errorCallback: function () {}
1042+
};
1043+
spyOn(options, 'successCallback');
1044+
var datavizWebmap = new WebMap(options);
1045+
1046+
setTimeout(() => {
1047+
expect(datavizWebmap.server).toBe(server);
1048+
expect(datavizWebmap.errorCallback).toBeDefined();
1049+
expect(datavizWebmap.credentialKey).toBeUndefined();
1050+
expect(datavizWebmap.credentialValue).toBeUndefined();
1051+
expect(options.successCallback).toHaveBeenCalled();
1052+
done();
1053+
}, 1000)
1054+
})
10081055
});

test/resources/WebMapV5.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)