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" ;
22import { reaction } from "mobx" ;
3- import { DerivedPropsGate } from "@mendix/widget-plugin-mobx-kit/main" ;
3+ import { ComputedAtom , DerivedPropsGate } from "@mendix/widget-plugin-mobx-kit/main" ;
44import { MapProviderEnum , MapsContainerProps } from "../../../typings/MapsProps" ;
55import { Marker } from "../../../typings/shared" ;
6- import { baseMapLayer } from "../../utils/leaflet" ;
76import { createLeafletMarker } from "../../utils/leaflet-markers" ;
87import { translateZoom } from "../../utils/zoom" ;
98import { 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 : "© <a href='https://osm.org/copyright'>OpenStreetMap</a> contributors" ,
39+ mapboxAttr :
40+ "Map data © <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 © 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
0 commit comments