Skip to content

Commit fd03eb0

Browse files
【update】ol webmap 支持 arcgis vectortile 并style 请求带上 baseUrl 参数 review by luox
1 parent b5615f9 commit fd03eb0

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4818,11 +4818,24 @@ export class WebMap extends Observable {
48184818
const envelope = this.getEnvelope(indexbounds, layerInfo.bounds);
48194819
const styleResolutions = this.getStyleResolutions(envelope);
48204820
// const origin = [envelope.left, envelope.top];
4821-
let baseUrl = layerInfo.url && layerInfo.url.split('?')[0];
4821+
let baseUrl = layerInfo.url;
4822+
let paramUrl = baseUrl.split('?')[1];
48224823
let spriteUrl = styles.sprite;
48234824
if (!CommonUtil.isAbsoluteURL(styles.sprite)) {
48244825
spriteUrl = CommonUtil.relative2absolute(styles.sprite, baseUrl);
48254826
}
4827+
if (layerInfo.dataSource.type === 'ARCGIS_VECTORTILE') {
4828+
Object.keys(styles.sources).forEach(function (key) {
4829+
Object.keys(styles.sources[key]).forEach(function(fieldName) {
4830+
if (fieldName === 'url') {
4831+
if (typeof styles.sources[key][fieldName] === 'string' && !CommonUtil.isAbsoluteURL(styles.sources[key][fieldName])) {
4832+
styles.sources[key][fieldName] = CommonUtil.relative2absolute(styles.sources[key][fieldName], baseUrl);
4833+
}
4834+
styles.sources[key][fieldName] = styles.sources[key][fieldName] + (paramUrl ? '?' + paramUrl + '&f=json' : '?f=json');
4835+
}
4836+
});
4837+
});
4838+
}
48264839
let withCredentials = this.isIportalProxyServiceUrl(spriteUrl);
48274840
// 创建MapBoxStyle样式
48284841
let mapboxStyles = new MapboxStyles({

src/openlayers/overlay/VectorTileSuperMapRest.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,15 @@ export class VectorTileSuperMapRest extends VectorTile {
323323
}
324324
if (style.sources && style.sources[source]) {
325325
let newUrl;
326+
let paramUrl = this.baseUrl && this.baseUrl.split('?')[1];
326327
if (style.sources[source].tiles) {
327328
newUrl = style.sources[source].tiles[0];
328329
if (!CommonUtil.isAbsoluteURL(newUrl)) {
329330
newUrl = CommonUtil.relative2absolute(newUrl, this.baseUrl);
330331
}
332+
if (paramUrl) {
333+
newUrl = CommonUtil.urlAppend(newUrl, paramUrl);
334+
}
331335
} else if (style.sources[source].url) {
332336
let tiles = style.sources[source].url;
333337
if (!CommonUtil.isAbsoluteURL(tiles)) {
@@ -340,6 +344,9 @@ export class VectorTileSuperMapRest extends VectorTile {
340344
tileUrl = CommonUtil.relative2absolute(tileUrl, tiles);
341345
}
342346
newUrl = SecurityManager.appendCredential(tileUrl);
347+
if (paramUrl) {
348+
newUrl = CommonUtil.urlAppend(newUrl, paramUrl);
349+
}
343350
}
344351
this._tileUrl = SecurityManager.appendCredential(newUrl);
345352
}

src/openlayers/overlay/vectortile/MapboxStyles.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,26 +377,34 @@ export class MapboxStyles extends Observable {
377377
return parts ? parts[1] + extension + (parts.length > 2 ? parts[2] : '') : url + extension;
378378
}
379379

380-
_handleRelativeUrl(styles, baseUrl) {
381-
if (!baseUrl) {
380+
_handleRelativeUrl(styles, url) {
381+
if (!url) {
382382
return styles;
383383
}
384+
const baseUrl = url.split('?')[0];
385+
const paramUrl = url.split('?')[1] || '';
384386
Object.keys(styles).forEach((fieldName) => {
385387
if (fieldName === 'sources') {
386388
Object.keys(styles[fieldName]).forEach((sourceName) => {
387-
this._handleRelativeUrl(styles[fieldName][sourceName], baseUrl);
389+
this._handleRelativeUrl(styles[fieldName][sourceName], url);
388390
})
389391
}
390392
if (fieldName === 'sprite' || fieldName === 'glyphs' || fieldName === 'url') {
391393
if (typeof styles[fieldName] === 'string' && !CommonUtil.isAbsoluteURL(styles[fieldName])) {
392394
styles[fieldName] = CommonUtil.relative2absolute(styles[fieldName], baseUrl);
393395
}
396+
if (paramUrl && !styles[fieldName].includes(paramUrl)) {
397+
styles[fieldName] = styles[fieldName] + (paramUrl ? (styles[fieldName].includes('?') ? '&' + paramUrl : '?' + paramUrl) : '');
398+
}
394399
}
395400
if (fieldName === 'tiles' && Array.isArray(styles[fieldName])) {
396401
styles[fieldName].forEach((tile) => {
397402
if (!CommonUtil.isAbsoluteURL(tile)) {
398403
tile = CommonUtil.relative2absolute(tile, baseUrl);
399404
}
405+
if (paramUrl && !tile.includes(paramUrl)) {
406+
tile = tile + (paramUrl ? (styles[fieldName].includes('?') ? '&' + paramUrl : '?' + paramUrl) : '');
407+
}
400408
})
401409
}
402410
})

test/openlayers/overlay/vectortile/MapboxStylesSpec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,58 @@ describe("openlayers_MapboxStyles", () => {
309309
}
310310
});
311311
});
312+
313+
it("handle relative url with param", done => {
314+
spyOn(XMLHttpRequest.prototype, 'send').and.callThrough();
315+
spyOn(XMLHttpRequest.prototype, 'setRequestHeader').and.callThrough();
316+
var style = {
317+
"version" : 8,
318+
"sprite" : "../sprites/sprite",
319+
"glyphs" : "../fonts/{fontstack}/{range}.pbf",
320+
"sources": {
321+
"esri": {
322+
"type": "vector",
323+
"url": "../../"
324+
}
325+
},
326+
"layers" : [{
327+
"id" : "Contour_11_main/0",
328+
"type" : "line",
329+
"source" : "esri",
330+
"source-layer" : "Contour",
331+
"filter" : ["all", ["==", "Index3", 1], ["==", "Index5", 1]],
332+
"minzoom" : 11,
333+
"maxzoom" : 12,
334+
"paint" : {
335+
"line-color" : "#61674a",
336+
"line-opacity" : 0.5,
337+
"line-width" : {
338+
"base" : 1.2,
339+
"stops" : [[11, 0.7], [16, 1.1]]
340+
}
341+
}
342+
}]
343+
}
344+
mapboxStyles = new MapboxStyles({
345+
style: style,
346+
baseUrl: 'http://localhost:9876?tkk=ddddssss',
347+
map: map,
348+
source: "California",
349+
headers:{'appToken':'test'}
350+
});
351+
mapboxStyles.on("styleloaded", () => {
352+
try {
353+
style = mapboxStyles.getStyleFunction();
354+
expect(style).not.toBeNull();
355+
expect(mapboxStyles._mbStyle.glyphs).toBe('http://localhost:9876/fonts/{fontstack}/{range}.pbf?tkk=ddddssss');
356+
expect(mapboxStyles._mbStyle.sprite).toBe('http://localhost:9876/sprites/sprite?tkk=ddddssss');
357+
expect(mapboxStyles._mbStyle.sources['esri']['url']).toBe('http://localhost:9876/?tkk=ddddssss');
358+
done();
359+
} catch (e) {
360+
console.log("'init_Style_headers'案例失败" + e.name + ":" + e.message);
361+
expect(false).toBeTruthy();
362+
done();
363+
}
364+
});
365+
});
312366
});

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