@@ -25,6 +25,7 @@ import TileGrid from 'ol/tilegrid/TileGrid';
2525 * @modulecategory Overlay
2626 * @param {Object } options - 参数。
2727 * @param {(string|undefined) } options.url - 服务地址。
28+ * @param {string } [options.baseUrl] - 当传入 style 对象且 style 中包含了相对路径时,需要传入 baseUrl 来拼接资源路径。
2829 * @param {(string|Object|undefined) } options.style - Mapbox Style JSON 对象或获取 Mapbox Style JSON 对象的 URL。当 `options.format` 为 {@link ol.format.MVT} 且 `options.source` 不为空时有效,优先级高于 `options.url`。
2930 * @param {(string|undefined) } options.source - Mapbox Style JSON 对象中的source名称。当 `options.style` 设置时有效。当不配置时,默认为 Mapbox Style JSON 的 `sources` 对象中的第一个。
3031 * @param {(string|Object) } [options.attributions='Tile Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a></span>'] - 版权信息。
@@ -58,8 +59,7 @@ export class VectorTileSuperMapRest extends VectorTile {
5859 projection : options . projection ,
5960 state :
6061 options . format instanceof MVT &&
61- options . style &&
62- Object . prototype . toString . call ( options . style ) == '[object String]'
62+ options . style
6363 ? 'loading'
6464 : options . state ,
6565 tileClass : options . tileClass ,
@@ -74,22 +74,9 @@ export class VectorTileSuperMapRest extends VectorTile {
7474 var me = this ;
7575 me . withCredentials = options . withCredentials ;
7676 me . _tileType = options . tileType || 'ScaleXY' ;
77+ me . baseUrl = options . baseUrl ;
7778 this . vectorTileStyles = new VectorTileStyles ( ) ;
78- if ( options . format instanceof MVT && options . style ) {
79- if ( Object . prototype . toString . call ( options . style ) == '[object String]' ) {
80- var url = SecurityManager . appendCredential ( options . style ) ;
81- FetchRequest . get ( url , null , { withCredentials : options . withCredentials } )
82- . then ( ( response ) => response . json ( ) )
83- . then ( ( mbStyle ) => {
84- this . _fillByStyleJSON ( mbStyle , options . source ) ;
85- this . setState ( 'ready' ) ;
86- } ) ;
87- } else {
88- this . _fillByStyleJSON ( options . style , options . source ) ;
89- }
90- } else {
91- this . _fillByRestMapOptions ( options . url , options ) ;
92- }
79+ this . _initialized ( options ) ;
9380
9481 function tileUrlFunction ( tileCoord , pixelRatio , projection ) {
9582 if ( ! me . tileGrid ) {
@@ -313,13 +300,48 @@ export class VectorTileSuperMapRest extends VectorTile {
313300 } ) ;
314301 }
315302 }
316- _fillByStyleJSON ( style , source ) {
303+
304+ async _initialized ( options ) {
305+ if ( options . format instanceof MVT && options . style ) {
306+ let style = options . style ;
307+ if ( Object . prototype . toString . call ( options . style ) == '[object String]' ) {
308+ var url = SecurityManager . appendCredential ( options . style ) ;
309+ const response = await FetchRequest . get ( url , null , {
310+ withCredentials : options . withCredentials
311+ } ) ;
312+ style = await response . json ( ) ;
313+ }
314+ await this . _fillByStyleJSON ( style , options . source ) ;
315+ } else {
316+ this . _fillByRestMapOptions ( options . url , options ) ;
317+ }
318+ this . setState ( 'ready' ) ;
319+ }
320+ async _fillByStyleJSON ( style , source ) {
317321 if ( ! source ) {
318322 source = Object . keys ( style . sources ) [ 0 ] ;
319323 }
320324 if ( style . sources && style . sources [ source ] ) {
321- //ToDo 支持多个tiles地址
322- this . _tileUrl = SecurityManager . appendCredential ( style . sources [ source ] . tiles [ 0 ] ) ;
325+ let newUrl ;
326+ if ( style . sources [ source ] . tiles ) {
327+ newUrl = style . sources [ source ] . tiles [ 0 ] ;
328+ if ( ! CommonUtil . isAbsoluteURL ( newUrl ) ) {
329+ newUrl = CommonUtil . relative2absolute ( newUrl , this . baseUrl ) ;
330+ }
331+ } else if ( style . sources [ source ] . url ) {
332+ let tiles = style . sources [ source ] . url ;
333+ if ( ! CommonUtil . isAbsoluteURL ( tiles ) ) {
334+ tiles = CommonUtil . relative2absolute ( tiles , this . baseUrl ) ;
335+ }
336+ const response = await FetchRequest . get ( tiles , { } , { withoutFormatSuffix : true } ) ;
337+ const sourceInfo = await response . json ( ) ;
338+ let tileUrl = sourceInfo . tiles [ 0 ] ;
339+ if ( ! CommonUtil . isAbsoluteURL ( tileUrl ) ) {
340+ tileUrl = CommonUtil . relative2absolute ( tileUrl , tiles ) ;
341+ }
342+ newUrl = SecurityManager . appendCredential ( tileUrl ) ;
343+ }
344+ this . _tileUrl = SecurityManager . appendCredential ( newUrl ) ;
323345 }
324346 if ( style . metadata && style . metadata . indexbounds ) {
325347 const indexbounds = style . metadata . indexbounds ;
0 commit comments