blob: f00aacc25171f0dfa34de062ce272788545f0417 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import type { StyleSpecification } from "react-map-gl/maplibre";
export async function loadStyle(styleName: string, colorScheme: string): Promise<StyleSpecification> {
const stylePath = `/maps/styles/${styleName}-${colorScheme}.json`;
const resp = await fetch(stylePath);
if (!resp.ok) {
throw new Error(`Failed to load style: ${stylePath}`);
}
const style = await resp.json();
const baseUrl = window.location.origin;
const spritePath = style.sprite;
// Handle both string and array cases for spritePath
if (Array.isArray(spritePath)) {
// For array format, update each sprite object's URL to be absolute
style.sprite = spritePath.map(spriteObj => {
const isAbsoluteUrl = spriteObj.url.startsWith("http://") || spriteObj.url.startsWith("https://");
if (isAbsoluteUrl) {
return spriteObj;
}
return {
...spriteObj,
url: `${baseUrl}${spriteObj.url}`
};
});
} else if (typeof spritePath === "string") {
if (!spritePath.startsWith("http://") && !spritePath.startsWith("https://")) {
style.sprite = `${baseUrl}${spritePath}`;
}
}
// Detect on each source if it the 'tiles' URLs are relative and convert them to absolute URLs
for (const sourceKey in style.sources) {
const source = style.sources[sourceKey];
for (const tileKey in source.tiles) {
const tileUrl = source.tiles[tileKey];
const isAbsoluteUrl = tileUrl.startsWith("http://") || tileUrl.startsWith("https://");
if (!isAbsoluteUrl) {
source.tiles[tileKey] = `${baseUrl}${tileUrl}`;
}
}
}
return style as StyleSpecification;
}
|