Skip to content

Commit 4c78e25

Browse files
iobuhovclaude
andcommitted
refactor(maps-web): move tile layer config to private method, remove tile-layer utility
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 30577c1 commit 4c78e25

2 files changed

Lines changed: 41 additions & 58 deletions

File tree

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { latLngBounds, Map as LeafletMapInstance, Marker as LeafletMarker, TileLayer } from "leaflet";
1+
import { latLngBounds, Map as LeafletMapInstance, Marker as LeafletMarker, TileLayer, TileLayerOptions } 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";
87
import { translateZoom } from "../../utils/zoom";
98
import { CurrentLocationService } from "../services/CurrentLocation.service";
109
import { LocationResolverService } from "../services/LocationResolver.service";
@@ -50,7 +49,7 @@ export class LeafletMapViewModel {
5049

5150
this.map = map;
5251

53-
const { url, options } = getTileLayerConfig(mapProvider, this.apiKeyAtom.get());
52+
const { url, options } = this.getTileLayerConfig(mapProvider);
5453
this.tileLayer = new TileLayer(url, options);
5554
this.tileLayer.addTo(map);
5655

@@ -73,6 +72,45 @@ export class LeafletMapViewModel {
7372
this.map = undefined;
7473
}
7574

75+
private getTileLayerConfig(mapProvider: MapProviderEnum): { url: string; options: TileLayerOptions } {
76+
const apiKey = this.apiKeyAtom.get();
77+
78+
if (mapProvider === "mapBox") {
79+
const token = apiKey ? `?access_token=${apiKey}` : "";
80+
return {
81+
url: `https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}${token}`,
82+
options: {
83+
attribution:
84+
"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>",
85+
id: "mapbox/streets-v11",
86+
tileSize: 512,
87+
zoomOffset: -1
88+
}
89+
};
90+
}
91+
92+
if (mapProvider === "hereMaps") {
93+
let token = "";
94+
if (apiKey) {
95+
if (apiKey.indexOf(",") > 0) {
96+
const [appId, appCode] = apiKey.split(",");
97+
token = `?app_id=${appId}&app_code=${appCode}`;
98+
} else {
99+
token = `?apiKey=${apiKey}`;
100+
}
101+
}
102+
return {
103+
url: `https://2.base.maps.cit.api.here.com/maptile/2.1/maptile/newest/normal.day/{z}/{x}/{y}/256/png8${token}`,
104+
options: { attribution: "Map &copy; 1987-2020 <a href='https://developer.here.com'>HERE</a>" }
105+
};
106+
}
107+
108+
return {
109+
url: "https://{s}.tile.osm.org/{z}/{x}/{y}.png",
110+
options: { attribution: "&copy; <a href='https://osm.org/copyright'>OpenStreetMap</a> contributors" }
111+
};
112+
}
113+
76114
private syncMarkers(locations: Marker[], currentLocation: Marker | undefined, autoZoom: boolean): void {
77115
const map = this.map;
78116
if (!map) {

packages/pluggableWidgets/maps-web/src/utils/tile-layer.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)