Skip to content

Commit 0d340b9

Browse files
iobuhovclaude
andcommitted
refactor(maps-web): extract tile layer config from ViewModel to utils/tile-layer.ts
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 51563de commit 0d340b9

2 files changed

Lines changed: 58 additions & 52 deletions

File tree

packages/pluggableWidgets/maps-web/src/model/viewmodels/LeafletMap.viewModel.ts

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { latLngBounds, Map as LeafletMapInstance, Marker as LeafletMarker, TileLayer, TileLayerOptions } from "leaflet";
1+
import { latLngBounds, Map as LeafletMapInstance, Marker as LeafletMarker, TileLayer } from "leaflet";
22
import { reaction } from "mobx";
33
import { ComputedAtom, DerivedPropsGate } from "@mendix/widget-plugin-mobx-kit/main";
44
import { MapProviderEnum, MapsContainerProps } from "../../../typings/MapsProps";
55
import { Marker } from "../../../typings/shared";
66
import { createLeafletMarker } from "../../utils/leaflet-markers";
7+
import { getTileLayerConfig } from "../../utils/tile-layer";
78
import { translateZoom } from "../../utils/zoom";
89
import { CurrentLocationService } from "../services/CurrentLocation.service";
910
import { LocationResolverService } from "../services/LocationResolver.service";
@@ -24,56 +25,6 @@ export class LeafletMapViewModel {
2425
return this.gate.props.mapProvider;
2526
}
2627

27-
private getTileLayerConfig(
28-
mapProvider: MapProviderEnum,
29-
apiKey: string | null
30-
): { url: string; options: TileLayerOptions } {
31-
const customUrls = {
32-
openStreetMap: "https://{s}.tile.osm.org/{z}/{x}/{y}.png",
33-
mapbox: "https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}",
34-
hereMaps: "https://2.base.maps.cit.api.here.com/maptile/2.1/maptile/newest/normal.day/{z}/{x}/{y}/256/png8"
35-
};
36-
37-
const mapAttr = {
38-
openStreetMapAttr: "&copy; <a href='https://osm.org/copyright'>OpenStreetMap</a> contributors",
39-
mapboxAttr:
40-
"Map data &copy; <a href='https://www.openstreetmap.org/'>OpenStreetMap</a> contributors, <a href='https://creativecommons.org/licenses/by-sa/2.0/'>CC-BY-SA</a>, Imagery © <a href='https://www.mapbox.com/'>Mapbox</a>",
41-
hereMapsAttr: "Map &copy; 1987-2020 <a href='https://developer.here.com'>HERE</a>"
42-
};
43-
44-
if (mapProvider === "mapBox") {
45-
const token = apiKey ? `?access_token=${apiKey}` : "";
46-
return {
47-
url: customUrls.mapbox + token,
48-
options: {
49-
attribution: mapAttr.mapboxAttr,
50-
id: "mapbox/streets-v11",
51-
tileSize: 512,
52-
zoomOffset: -1
53-
}
54-
};
55-
} else if (mapProvider === "hereMaps") {
56-
let token = "";
57-
if (apiKey) {
58-
if (apiKey.indexOf(",") > 0) {
59-
const [appId, appCode] = apiKey.split(",");
60-
token = `?app_id=${appId}&app_code=${appCode}`;
61-
} else {
62-
token = `?apiKey=${apiKey}`;
63-
}
64-
}
65-
return {
66-
url: customUrls.hereMaps + token,
67-
options: { attribution: mapAttr.hereMapsAttr }
68-
};
69-
} else {
70-
return {
71-
url: customUrls.openStreetMap,
72-
options: { attribution: mapAttr.openStreetMapAttr }
73-
};
74-
}
75-
}
76-
7728
setupMap(node: HTMLDivElement): () => void {
7829
const {
7930
attributionControl,
@@ -98,7 +49,7 @@ export class LeafletMapViewModel {
9849

9950
this.map = map;
10051

101-
const { url, options } = this.getTileLayerConfig(mapProvider, this.apiKeyAtom.get());
52+
const { url, options } = getTileLayerConfig(mapProvider, this.apiKeyAtom.get());
10253
this.tileLayer = new TileLayer(url, options);
10354
this.tileLayer.addTo(map);
10455

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { TileLayerOptions } from "leaflet";
2+
import { MapProviderEnum } from "../../typings/MapsProps";
3+
4+
export interface TileLayerConfig {
5+
url: string;
6+
options: TileLayerOptions;
7+
}
8+
9+
const urls = {
10+
openStreetMap: "https://{s}.tile.osm.org/{z}/{x}/{y}.png",
11+
mapbox: "https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}",
12+
hereMaps: "https://2.base.maps.cit.api.here.com/maptile/2.1/maptile/newest/normal.day/{z}/{x}/{y}/256/png8"
13+
};
14+
15+
const attributions = {
16+
openStreetMap: "&copy; <a href='https://osm.org/copyright'>OpenStreetMap</a> contributors",
17+
mapbox: "Map data &copy; <a href='https://www.openstreetmap.org/'>OpenStreetMap</a> contributors, <a href='https://creativecommons.org/licenses/by-sa/2.0/'>CC-BY-SA</a>, Imagery © <a href='https://www.mapbox.com/'>Mapbox</a>",
18+
hereMaps: "Map &copy; 1987-2020 <a href='https://developer.here.com'>HERE</a>"
19+
};
20+
21+
export function getTileLayerConfig(mapProvider: MapProviderEnum, apiKey: string | null): TileLayerConfig {
22+
if (mapProvider === "mapBox") {
23+
const token = apiKey ? `?access_token=${apiKey}` : "";
24+
return {
25+
url: urls.mapbox + token,
26+
options: {
27+
attribution: attributions.mapbox,
28+
id: "mapbox/streets-v11",
29+
tileSize: 512,
30+
zoomOffset: -1
31+
}
32+
};
33+
}
34+
35+
if (mapProvider === "hereMaps") {
36+
let token = "";
37+
if (apiKey) {
38+
if (apiKey.indexOf(",") > 0) {
39+
const [appId, appCode] = apiKey.split(",");
40+
token = `?app_id=${appId}&app_code=${appCode}`;
41+
} else {
42+
token = `?apiKey=${apiKey}`;
43+
}
44+
}
45+
return {
46+
url: urls.hereMaps + token,
47+
options: { attribution: attributions.hereMaps }
48+
};
49+
}
50+
51+
return {
52+
url: urls.openStreetMap,
53+
options: { attribution: attributions.openStreetMap }
54+
};
55+
}

0 commit comments

Comments
 (0)