Skip to content

Commit 228487c

Browse files
committed
Fix streetview breaking on some basemaps
1 parent c552b5f commit 228487c

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

web/src/common/highlight_roads.ts

+27-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import type { Map } from "maplibre-gl";
22

3-
// Each MapTiler basemap style uses different layer IDs for roads and paths
4-
export function getRoadLayerNames(map: Map, maptilerStyle: string): string[] {
5-
if (maptilerStyle == "dataviz") {
6-
return ["Road network", "Path"];
3+
// Each basemap style uses different layer IDs for roads and paths
4+
export function getRoadLayerNames(map: Map, mapStyle: string): string[] {
5+
// The styles may change over time. Guarantee we only return valid line layers.
6+
let availableLayers = new Set(
7+
map
8+
.getStyle()
9+
.layers.filter((l) => l.type == "line")
10+
.map((l) => l.id),
11+
);
12+
13+
if (mapStyle == "dataviz") {
14+
return ["Road network", "Path"].filter((l) => availableLayers.has(l));
15+
}
16+
if (mapStyle == "hybrid") {
17+
return ["Path", "Road", "Tunnel"].filter((l) => availableLayers.has(l));
718
}
8-
if (maptilerStyle == "streets") {
19+
if (mapStyle == "streets") {
920
let layers = [];
1021
for (let outer of ["road", "bridge", "tunnel"]) {
1122
for (let inner of [
@@ -28,19 +39,17 @@ export function getRoadLayerNames(map: Map, maptilerStyle: string): string[] {
2839
layers.push(`${outer}_${inner}`);
2940
}
3041
}
31-
return layers;
42+
return layers.filter((l) => availableLayers.has(l));
3243
}
33-
if (maptilerStyle == "hybrid") {
34-
return ["Path", "Road", "Tunnel"];
44+
if (mapStyle == "uk-openzoomstack-light") {
45+
return map
46+
.getStyle()
47+
.layers.filter(
48+
// @ts-expect-error source-layer is present
49+
(layer) => layer["source-layer"] == "roads" && layer.type == "line",
50+
)
51+
.map((layer) => layer.id);
3552
}
36-
if (maptilerStyle == "uk-openzoomstack-light") {
37-
return (
38-
map
39-
.getStyle()
40-
// @ts-expect-error It does exist
41-
.layers.filter((layer) => layer["source-layer"] == "roads")
42-
.map((layer) => layer.id)
43-
);
44-
}
45-
throw new Error(`Unknown style ${maptilerStyle}`);
53+
54+
return [];
4655
}

0 commit comments

Comments
 (0)