Skip to content

Commit 3b4d398

Browse files
iobuhovclaude
andcommitted
refactor(maps-web): inline tile layer logic in LeafletMapViewModel, remove baseMapLayer utility
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 5eca563 commit 3b4d398

3 files changed

Lines changed: 56 additions & 65 deletions

File tree

packages/pluggableWidgets/maps-web/src/model/containers/Maps.container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const _01_coreBindings: BindingGroup = {
3232
inject() {
3333
injected(LocationResolverService, CORE.setupService, CORE.mainGate, CORE.geocodeFunction, CORE.geodecodeApiKey);
3434
injected(CurrentLocationService, CORE.setupService, CORE.config, CORE.getLocationFunction);
35-
injected(LeafletMapViewModel, CORE.mainGate, MAPS.locationResolver, MAPS.currentLocation);
35+
injected(LeafletMapViewModel, CORE.mainGate, MAPS.locationResolver, MAPS.currentLocation, CORE.apiKey);
3636
},
3737
define(container) {
3838
container.bind(MAPS.locationResolver).toInstance(LocationResolverService).inSingletonScope();

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

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
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";
3-
import { DerivedPropsGate } from "@mendix/widget-plugin-mobx-kit/main";
3+
import { ComputedAtom, DerivedPropsGate } from "@mendix/widget-plugin-mobx-kit/main";
44
import { MapProviderEnum, MapsContainerProps } from "../../../typings/MapsProps";
55
import { Marker } from "../../../typings/shared";
6-
import { baseMapLayer } from "../../utils/leaflet";
76
import { createLeafletMarker } from "../../utils/leaflet-markers";
87
import { translateZoom } from "../../utils/zoom";
98
import { CurrentLocationService } from "../services/CurrentLocation.service";
@@ -17,13 +16,64 @@ export class LeafletMapViewModel {
1716
constructor(
1817
private readonly gate: DerivedPropsGate<MapsContainerProps>,
1918
private readonly locationResolver: LocationResolverService,
20-
private readonly currentLocationService: CurrentLocationService
19+
private readonly currentLocationService: CurrentLocationService,
20+
private readonly apiKeyAtom: ComputedAtom<string | null>
2121
) {}
2222

2323
get mapProvider(): MapProviderEnum {
2424
return this.gate.props.mapProvider;
2525
}
2626

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+
2777
setupMap(node: HTMLDivElement): () => void {
2878
const {
2979
attributionControl,
@@ -48,10 +98,7 @@ export class LeafletMapViewModel {
4898

4999
this.map = map;
50100

51-
const { url, ...options } = baseMapLayer(
52-
mapProvider,
53-
this.gate.props.apiKeyExp?.value ?? this.gate.props.apiKey
54-
);
101+
const { url, options } = this.getTileLayerConfig(mapProvider, this.apiKeyAtom.get());
55102
this.tileLayer = new TileLayer(url, options);
56103
this.tileLayer.addTo(map);
57104

packages/pluggableWidgets/maps-web/src/utils/leaflet.ts

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

0 commit comments

Comments
 (0)