33 * which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
44import { FetchRequest } from '@supermapgis/iclient-common/util/FetchRequest' ;
55import { SecurityManager } from '@supermapgis/iclient-common/security/SecurityManager' ;
6- import { Util as CommonUtil } from '@supermapgis/iclient-common/commontypes/Util' ;
6+ import { Util as CommonUtil } from '@supermapgis/iclient-common/commontypes/Util' ;
77import { olExtends } from './olExtends' ;
88import remove from 'lodash.remove' ;
99import Observable from 'ol/Observable' ;
@@ -25,6 +25,7 @@ import Text from 'ol/style/Text';
2525 * @param {Object } options - 参数。
2626 * @param {(string|undefined) } [options.url] - SuperMap iServer 地图服务地址,例如'http://localhost:8090/iserver/services/map-mvt-test/rest/maps/test',与 options.style 互斥,优先级低于 options.style。
2727 * @param {(Object|string|undefined) } [options.style] - Mapbox Style JSON 对象或获取 Mapbox Style JSON 对象的 URL。与 options.url 互斥,优先级高于 options.url。
28+ * @param {string } [options.baseUrl] - 当传入 style 对象且 style 中包含了相对路径时,需要传入 baseUrl 来拼接资源路径。
2829 * @param {Array.<number> } [options.resolutions] - 地图分辨率数组,用于映射 zoom 值。通常情況与地图的 {@link ol.View} 的分辨率一致。</br>
2930 * 默认值为:[78271.51696402048,39135.75848201024, 19567.87924100512,9783.93962050256,4891.96981025128,2445.98490512564, 1222.99245256282,611.49622628141,305.748113140705,152.8740565703525, 76.43702828517625,38.21851414258813,19.109257071294063,9.554628535647032, 4.777314267823516,2.388657133911758,1.194328566955879,0.5971642834779395, 0.29858214173896974,0.14929107086948487,0.07464553543474244]。
3031 * @param {(string|Array.<string>|undefined) } [options.source] - Mapbox Style 'source'的 key 值或者 'layer' 的 ID 数组。
@@ -103,6 +104,7 @@ export class MapboxStyles extends Observable {
103104 } ) ;
104105 } ;
105106 this . layersBySourceLayer = { } ;
107+ this . baseUrl = options . baseUrl ;
106108 olExtends ( this . map ) ;
107109 this . _loadStyle ( this . styleTarget ) ;
108110 }
@@ -248,6 +250,7 @@ export class MapboxStyles extends Observable {
248250 }
249251 _loadStyle ( style ) {
250252 if ( Object . prototype . toString . call ( style ) == '[object Object]' ) {
253+ this . _handleRelativeUrl ( style , this . baseUrl ) ;
251254 this . _mbStyle = style ;
252255 setTimeout ( ( ) => {
253256 this . _resolve ( ) ;
@@ -257,6 +260,7 @@ export class MapboxStyles extends Observable {
257260 FetchRequest . get ( url , null , { withCredentials : this . withCredentials , headers : this . headers } )
258261 . then ( response => response . json ( ) )
259262 . then ( mbStyle => {
263+ this . _handleRelativeUrl ( mbStyle , url ) ;
260264 this . _mbStyle = mbStyle ;
261265 this . _resolve ( ) ;
262266 } ) ;
@@ -288,7 +292,7 @@ export class MapboxStyles extends Observable {
288292 xhr . responseType = 'blob' ;
289293 xhr . addEventListener ( 'loadend' , ( e ) => {
290294 var data = e . target . response ;
291- if ( data !== undefined ) {
295+ if ( data !== undefined && data !== null ) {
292296 const img = new Image ( ) ;
293297 img . src = URL . createObjectURL ( data ) ;
294298 this . _spriteImage = img ;
@@ -392,4 +396,29 @@ export class MapboxStyles extends Observable {
392396 const parts = url . match ( this . spriteRegEx ) ;
393397 return parts ? parts [ 1 ] + extension + ( parts . length > 2 ? parts [ 2 ] : '' ) : url + extension ;
394398 }
399+
400+ _handleRelativeUrl ( styles , baseUrl ) {
401+ if ( ! baseUrl ) {
402+ return styles ;
403+ }
404+ Object . keys ( styles ) . forEach ( ( fieldName ) => {
405+ if ( fieldName === 'sources' ) {
406+ Object . keys ( styles [ fieldName ] ) . forEach ( ( sourceName ) => {
407+ this . _handleRelativeUrl ( styles [ fieldName ] [ sourceName ] , baseUrl ) ;
408+ } )
409+ }
410+ if ( fieldName === 'sprite' || fieldName === 'glyphs' || fieldName === 'url' ) {
411+ if ( typeof styles [ fieldName ] === 'string' && ! CommonUtil . isAbsoluteURL ( styles [ fieldName ] ) ) {
412+ styles [ fieldName ] = CommonUtil . relative2absolute ( styles [ fieldName ] , baseUrl ) ;
413+ }
414+ }
415+ if ( fieldName === 'tiles' && Array . isArray ( styles [ fieldName ] ) ) {
416+ styles [ fieldName ] . forEach ( ( tile ) => {
417+ if ( ! CommonUtil . isAbsoluteURL ( tile ) ) {
418+ tile = CommonUtil . relative2absolute ( tile , baseUrl ) ;
419+ }
420+ } )
421+ }
422+ } )
423+ }
395424}
0 commit comments