Skip to content

Commit d87b7bb

Browse files
author
ChenGuanglin
committed
【feature】对接serverId的mapboxstyle样式,UT待测试
1 parent 1df62cd commit d87b7bb

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

src/mapboxgl/mapping/WebMap.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,56 @@ export class WebMap extends mapboxgl.Evented {
344344
url = mapUrls[layerType];
345345
this._createXYZLayer(layerInfo, url);
346346
break;
347+
case 'MAPBOXSTYLE':
348+
this._createMapboxStyle(layerInfo);
349+
break;
347350
default:
348351
break;
349352
}
350353
}
351354

355+
/**
356+
* @private
357+
* @function mapboxgl.supermap.WebMap.prototype._createMapboxStyle
358+
* @description 创建 Mapbox 样式。
359+
* @param {Object} mapInfo - map 信息。
360+
*/
361+
_createMapboxStyle(mapInfo) {
362+
let _this = this,
363+
{ dataSource = {} } = mapInfo,
364+
{ serverId, url } = dataSource,
365+
styleUrl;
366+
styleUrl = serverId !== undefined ? `${this.server}web/datas/${serverId}/download` : url;
367+
FetchRequest.get(styleUrl, null, {
368+
withCredentials: this.withCredentials,
369+
withoutFormatSuffix: true,
370+
headers: {
371+
'Content-Type': 'application/json;chartset=uft-8'
372+
}
373+
}).then(response => {
374+
return response.json();
375+
}).then(style => {
376+
_this._matchStyleObject(style);
377+
_this.map.setStyle(style);
378+
})
379+
}
380+
381+
/**
382+
* @private
383+
* @function mapboxgl.supermap.WebMap.prototype._matchStyleObject
384+
* @description 恢复 style 为标准格式。
385+
* @param {Object} style - mapbox 样式。
386+
*/
387+
_matchStyleObject(style) {
388+
let { sprite, glyphs } = style;
389+
if (sprite && typeof sprite === 'object'){
390+
style.sprite = Object.values(sprite)[0];
391+
}
392+
if (glyphs && typeof glyphs === 'object'){
393+
style.glyphs = Object.values(glyphs)[0];
394+
}
395+
}
396+
352397
/**
353398
* @private
354399
* @function mapboxgl.supermap.WebMap.prototype._createTiandituLayer

src/openlayers/mapping/WebMap.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,20 +3378,31 @@ export class WebMap extends Observable {
33783378
* @param {object} mapInfo - 地图信息
33793379
*/
33803380
getMBStyle(mapInfo) {
3381-
let baseLayer = mapInfo.baseLayer,
3382-
url = baseLayer.dataSource.url,
3383-
layerInfo = {};
3384-
return FetchRequest.get(this.getRequestUrl(url)).then(result => {
3381+
let _this = this,
3382+
baseLayer = mapInfo.baseLayer,
3383+
dataSource = baseLayer.dataSource || {},
3384+
{ url, serverId } = dataSource,
3385+
layerInfo = {},
3386+
styleUrl;
3387+
styleUrl = serverId !== undefined ? `${this.server}web/datas/${serverId}/download` : url;
3388+
return FetchRequest.get(this.getRequestUrl(styleUrl), null, {
3389+
withCredentials: this.withCredentials,
3390+
withoutFormatSuffix: true,
3391+
headers: {
3392+
'Content-Type': 'application/json;chartset=uft-8'
3393+
}
3394+
}).then(result => {
33853395
return result.json();
33863396
}).then(styles => {
3397+
_this._matchStyleObject(styles);
33873398
let extent = styles.metadata.mapbounds;
33883399
baseLayer.extent = extent; // 这里把extent保存一下
33893400

33903401
layerInfo.projection = mapInfo.projection,
33913402
layerInfo.epsgCode = mapInfo.projection,
33923403
layerInfo.visible = baseLayer.visible,
33933404
layerInfo.name = baseLayer.name,
3394-
layerInfo.url = url,
3405+
layerInfo.url = styleUrl,
33953406
layerInfo.sourceType = 'VECTOR_TILE',
33963407
layerInfo.layerType = 'VECTOR_TILE',
33973408
layerInfo.styles = styles,
@@ -3408,6 +3419,23 @@ export class WebMap extends Observable {
34083419
})
34093420
}
34103421

3422+
/**
3423+
* @private
3424+
* @function mapboxgl.supermap.WebMap.prototype._matchStyleObject
3425+
* @description 恢复 style 为标准格式。
3426+
* @param {Object} style - mapbox 样式。
3427+
*/
3428+
_matchStyleObject(style) {
3429+
let { sprite, glyphs } = style;
3430+
if (sprite && typeof sprite === 'object'){
3431+
style.sprite = Object.values(sprite)[0];
3432+
}
3433+
if (glyphs && typeof glyphs === 'object'){
3434+
style.glyphs = Object.values(glyphs)[0];
3435+
}
3436+
}
3437+
3438+
34113439
/**
34123440
* @private
34133441
* @function ol.supermap.WebMap.prototype.createMVTLayer

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