Skip to content

Commit 5244d77

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

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
@@ -5168,11 +5168,24 @@ export class WebMap extends Observable {
51685168
const envelope = this.getEnvelope(indexbounds, layerInfo.bounds);
51695169
const styleResolutions = this.getStyleResolutions(envelope);
51705170
// const origin = [envelope.left, envelope.top];
5171-
let baseUrl = layerInfo.url && layerInfo.url.split('?')[0];
5171+
let baseUrl = layerInfo.url;
5172+
let paramUrl = baseUrl.split('?')[1];
51725173
let spriteUrl = styles.sprite;
51735174
if (!CommonUtil.isAbsoluteURL(styles.sprite)) {
51745175
spriteUrl = CommonUtil.relative2absolute(styles.sprite, baseUrl);
51755176
}
5177+
if (layerInfo.dataSource.type === 'ARCGIS_VECTORTILE') {
5178+
Object.keys(styles.sources).forEach(function (key) {
5179+
Object.keys(styles.sources[key]).forEach(function(fieldName) {
5180+
if (fieldName === 'url') {
5181+
if (typeof styles.sources[key][fieldName] === 'string' && !CommonUtil.isAbsoluteURL(styles.sources[key][fieldName])) {
5182+
styles.sources[key][fieldName] = CommonUtil.relative2absolute(styles.sources[key][fieldName], baseUrl);
5183+
}
5184+
styles.sources[key][fieldName] = styles.sources[key][fieldName] + (paramUrl ? '?' + paramUrl + '&f=json' : '?f=json');
5185+
}
5186+
});
5187+
});
5188+
}
51765189
let withCredentials = this.isIportalProxyServiceUrl(spriteUrl);
51775190
const requestParameters = this.tileRequestParameters && this.tileRequestParameters(spriteUrl);
51785191
// 创建MapBoxStyle样式

src/openlayers/overlay/VectorTileSuperMapRest.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,15 @@ export class VectorTileSuperMapRest extends VectorTile {
338338
//ToDo 支持多个tiles地址
339339
if (style.sources && style.sources[source]) {
340340
let newUrl;
341+
let paramUrl = this.baseUrl && this.baseUrl.split('?')[1];
341342
if (style.sources[source].tiles) {
342343
newUrl = style.sources[source].tiles[0];
343344
if (!CommonUtil.isAbsoluteURL(newUrl)) {
344345
newUrl = CommonUtil.relative2absolute(newUrl, this.baseUrl);
345346
}
347+
if (paramUrl) {
348+
newUrl = CommonUtil.urlAppend(newUrl, paramUrl);
349+
}
346350
} else if (style.sources[source].url) {
347351
let tiles = style.sources[source].url;
348352
if (!CommonUtil.isAbsoluteURL(tiles)) {
@@ -355,6 +359,9 @@ export class VectorTileSuperMapRest extends VectorTile {
355359
tileUrl = CommonUtil.relative2absolute(tileUrl, tiles);
356360
}
357361
newUrl = SecurityManager.appendCredential(tileUrl);
362+
if (paramUrl) {
363+
newUrl = CommonUtil.urlAppend(newUrl, paramUrl);
364+
}
358365
}
359366
this._tileUrl = SecurityManager.appendCredential(newUrl);
360367
}

src/openlayers/overlay/vectortile/MapboxStyles.js

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

400-
_handleRelativeUrl(styles, baseUrl) {
401-
if (!baseUrl) {
400+
_handleRelativeUrl(styles, url) {
401+
if (!url) {
402402
return styles;
403403
}
404+
const baseUrl = url.split('?')[0];
405+
const paramUrl = url.split('?')[1] || '';
404406
Object.keys(styles).forEach((fieldName) => {
405407
if (fieldName === 'sources') {
406408
Object.keys(styles[fieldName]).forEach((sourceName) => {
407-
this._handleRelativeUrl(styles[fieldName][sourceName], baseUrl);
409+
this._handleRelativeUrl(styles[fieldName][sourceName], url);
408410
})
409411
}
410412
if (fieldName === 'sprite' || fieldName === 'glyphs' || fieldName === 'url') {
411413
if (typeof styles[fieldName] === 'string' && !CommonUtil.isAbsoluteURL(styles[fieldName])) {
412414
styles[fieldName] = CommonUtil.relative2absolute(styles[fieldName], baseUrl);
413415
}
416+
if (paramUrl && !styles[fieldName].includes(paramUrl)) {
417+
styles[fieldName] = styles[fieldName] + (paramUrl ? (styles[fieldName].includes('?') ? '&' + paramUrl : '?' + paramUrl) : '');
418+
}
414419
}
415420
if (fieldName === 'tiles' && Array.isArray(styles[fieldName])) {
416421
styles[fieldName].forEach((tile) => {
417422
if (!CommonUtil.isAbsoluteURL(tile)) {
418423
tile = CommonUtil.relative2absolute(tile, baseUrl);
419424
}
425+
if (paramUrl && !tile.includes(paramUrl)) {
426+
tile = tile + (paramUrl ? (styles[fieldName].includes('?') ? '&' + paramUrl : '?' + paramUrl) : '');
427+
}
420428
})
421429
}
422430
})

test/openlayers/overlay/vectortile/MapboxStylesSpec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,58 @@ describe("openlayers_MapboxStyles", () => {
334334
}
335335
});
336336
});
337+
338+
it("handle relative url with param", done => {
339+
spyOn(XMLHttpRequest.prototype, 'send').and.callThrough();
340+
spyOn(XMLHttpRequest.prototype, 'setRequestHeader').and.callThrough();
341+
var style = {
342+
"version" : 8,
343+
"sprite" : "../sprites/sprite",
344+
"glyphs" : "../fonts/{fontstack}/{range}.pbf",
345+
"sources": {
346+
"esri": {
347+
"type": "vector",
348+
"url": "../../"
349+
}
350+
},
351+
"layers" : [{
352+
"id" : "Contour_11_main/0",
353+
"type" : "line",
354+
"source" : "esri",
355+
"source-layer" : "Contour",
356+
"filter" : ["all", ["==", "Index3", 1], ["==", "Index5", 1]],
357+
"minzoom" : 11,
358+
"maxzoom" : 12,
359+
"paint" : {
360+
"line-color" : "#61674a",
361+
"line-opacity" : 0.5,
362+
"line-width" : {
363+
"base" : 1.2,
364+
"stops" : [[11, 0.7], [16, 1.1]]
365+
}
366+
}
367+
}]
368+
}
369+
mapboxStyles = new MapboxStyles({
370+
style: style,
371+
baseUrl: 'http://localhost:9876?tkk=ddddssss',
372+
map: map,
373+
source: "California",
374+
headers:{'appToken':'test'}
375+
});
376+
mapboxStyles.on("styleloaded", () => {
377+
try {
378+
style = mapboxStyles.getStyleFunction();
379+
expect(style).not.toBeNull();
380+
expect(mapboxStyles._mbStyle.glyphs).toBe('http://localhost:9876/fonts/{fontstack}/{range}.pbf?tkk=ddddssss');
381+
expect(mapboxStyles._mbStyle.sprite).toBe('http://localhost:9876/sprites/sprite?tkk=ddddssss');
382+
expect(mapboxStyles._mbStyle.sources['esri']['url']).toBe('http://localhost:9876/?tkk=ddddssss');
383+
done();
384+
} catch (e) {
385+
console.log("'init_Style_headers'案例失败" + e.name + ":" + e.message);
386+
expect(false).toBeTruthy();
387+
done();
388+
}
389+
});
390+
});
337391
});

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