Skip to content

Commit 4880ddb

Browse files
committed
【update】1) webMap支持用户传入tileFormat,作为瓦片出图的方式
(reviewed by chengl)
1 parent 35e9a4a commit 4880ddb

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const dpiConfig = {
8585
* @param {Object} [options.serviceProxy] - iportal内置代理信息, 仅矢量瓦片图层上图才会使用
8686
* @param {string} [options.tiandituKey] - 天地图的key
8787
* @param {string} [options.proxy] - 代理地址,当域名不一致,请求会加上代理。避免跨域
88+
* @param {string} [options.tileFormat] - 地图瓦片出图格式,png/webp
8889
* @param {function} [options.mapSetting.mapClickCallback] - 地图被点击的回调函数
8990
* @param {function} [options.mapSetting.overlays] - 地图的overlayer
9091
* @param {function} [options.mapSetting.controls] - 地图的控件
@@ -118,6 +119,7 @@ export class WebMap extends Observable {
118119
this.layers = [];
119120
this.events = new Events(this, null, ["updateDataflowFeature"], true);
120121
this.webMap = options.webMap;
122+
this.tileFormat = options.tileFormat;
121123
this.createMap(options.mapSetting);
122124
if (this.webMap) {
123125
// webmap有可能是url地址,有可能是webmap对象
@@ -1235,7 +1237,7 @@ export class WebMap extends Observable {
12351237
*/
12361238
getTileLayerExtentInfo(layerInfo, isDynamic = true) {
12371239
let that = this,
1238-
// token,
1240+
token,
12391241
url = layerInfo.url.trim(),
12401242
credential = layerInfo.credential,
12411243
options = {
@@ -1253,22 +1255,26 @@ export class WebMap extends Observable {
12531255
}
12541256
if (credential) {
12551257
url = `${url}&token=${encodeURI(credential.token)}`;
1256-
// token = credential.token;
1258+
token = credential.token;
12571259
}
12581260
return FetchRequest.get(that.getRequestUrl(`${url}.json`), null, options).then(function (response) {
12591261
return response.json();
12601262
}).then(async (result) => {
12611263
if (result.succeed === false) {
12621264
return result
12631265
}
1264-
// let isSupportWebp = await that.isSupportWebp(layerInfo.url, token);
1266+
let format = 'png';
1267+
if(that.tileFormat === 'WebP') {
1268+
const isSupportWebp = await that.isSupportWebp(layerInfo.url, token);
1269+
format = isSupportWebp ? 'webp' : 'png';
1270+
}
12651271
return {
12661272
units: result.coordUnit && result.coordUnit.toLowerCase(),
12671273
coordUnit: result.coordUnit,
12681274
visibleScales: result.visibleScales,
12691275
extent: [result.bounds.left, result.bounds.bottom, result.bounds.right, result.bounds.top],
12701276
projection: `EPSG:${result.prjCoordSys.epsgCode}`,
1271-
format: 'png'
1277+
format
12721278
}
12731279
}).catch((error) => {
12741280
return {
@@ -1298,7 +1304,7 @@ export class WebMap extends Observable {
12981304
}
12991305
return FetchRequest.get(that.getRequestUrl(`${layerInfo.url}.json`), null, options).then(function (response) {
13001306
return response.json();
1301-
}).then(function (result) {
1307+
}).then(async function (result) {
13021308
// layerInfo.projection = mapInfo.projection;
13031309
// layerInfo.extent = [mapInfo.extent.leftBottom.x, mapInfo.extent.leftBottom.y, mapInfo.extent.rightTop.x, mapInfo.extent.rightTop.y];
13041310
// 比例尺 单位
@@ -1311,11 +1317,13 @@ export class WebMap extends Observable {
13111317
}
13121318
layerInfo.maxZoom = result.maxZoom;
13131319
layerInfo.maxZoom = result.minZoom;
1314-
// let token = layerInfo.credential ? layerInfo.credential.token : undefined;
1315-
// let isSupprtWebp = await that.isSupportWebp(layerInfo.url, token);
1316-
// eslint-disable-next-line require-atomic-updates
1317-
// layerInfo.format = isSupprtWebp ? 'webp' : 'png';
1320+
let token = layerInfo.credential ? layerInfo.credential.token : undefined;
13181321
layerInfo.format = 'png';
1322+
// china_dark为默认底图,还是用png出图
1323+
if(that.tileFormat === 'WebP' && layerInfo.url !== 'https://maptiles.supermapol.com/iserver/services/map_China/rest/maps/China_Dark') {
1324+
const isSupprtWebp = await that.isSupportWebp(layerInfo.url, token);
1325+
layerInfo.format = isSupprtWebp ? 'webp' : 'png';
1326+
}
13191327
// 请求结果完成 继续添加图层
13201328
if (mapInfo) {
13211329
//todo 这个貌似没有用到,下次优化
@@ -4597,36 +4605,36 @@ export class WebMap extends Observable {
45974605
* @param {*} token 服务token
45984606
* @returns {boolean}
45994607
*/
4600-
// isSupportWebp(url, token) {
4601-
// // 还需要判断浏览器
4602-
// let isIE = this.isIE();
4603-
// if (isIE || (this.isFirefox() && this.getFirefoxVersion() < 65) ||
4604-
// (this.isChrome() && this.getChromeVersion() < 32)) {
4605-
// return false;
4606-
// }
4607-
// url = token ? `${url}/tileImage.webp?token=${token}` : `${url}/tileImage.webp`;
4608-
// let isSameDomain = CommonUtil.isInTheSameDomain(url), excledeCreditial;
4609-
// if (isSameDomain && !token) {
4610-
// // online上服务域名一直,要用token值
4611-
// excledeCreditial = false;
4612-
// } else {
4613-
// excledeCreditial = true;
4614-
// }
4615-
// url = this.getRequestUrl(url, excledeCreditial);
4616-
// return FetchRequest.get(url, null, {
4617-
// withCredentials: this.withCredentials,
4618-
// withoutFormatSuffix: true
4619-
// }).then(function (response) {
4620-
// if (response.status !== 200) {
4621-
// throw response.status;
4622-
// }
4623-
// return response;
4624-
// }).then(() => {
4625-
// return true;
4626-
// }).catch(() => {
4627-
// return false;
4628-
// })
4629-
// }
4608+
isSupportWebp(url, token) {
4609+
// 还需要判断浏览器
4610+
let isIE = this.isIE();
4611+
if (isIE || (this.isFirefox() && this.getFirefoxVersion() < 65) ||
4612+
(this.isChrome() && this.getChromeVersion() < 32)) {
4613+
return false;
4614+
}
4615+
url = token ? `${url}/tileImage.webp?token=${token}` : `${url}/tileImage.webp`;
4616+
let isSameDomain = CommonUtil.isInTheSameDomain(url), excledeCreditial;
4617+
if (isSameDomain && !token) {
4618+
// online上服务域名一直,要用token值
4619+
excledeCreditial = false;
4620+
} else {
4621+
excledeCreditial = true;
4622+
}
4623+
url = this.getRequestUrl(url, excledeCreditial);
4624+
return FetchRequest.get(url, null, {
4625+
withCredentials: this.withCredentials,
4626+
withoutFormatSuffix: true
4627+
}).then(function (response) {
4628+
if (response.status !== 200) {
4629+
throw response.status;
4630+
}
4631+
return response;
4632+
}).then(() => {
4633+
return true;
4634+
}).catch(() => {
4635+
return false;
4636+
})
4637+
}
46304638
/**
46314639
* @private
46324640
* @function ol.supermap.WebMap.prototype.isIE

0 commit comments

Comments
 (0)