maplibre-gl-basemap-control 0.14.0 → 0.14.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -152,6 +152,7 @@ destructive style change, so the missing key can be entered and the basemap retr
152
152
  const control = new BasemapControl({
153
153
  defaultBasemapId: 'maptiler-streets',
154
154
  mapTilerApiKey: 'YOUR_MAPTILER_API_KEY',
155
+ cartoApiKey: 'YOUR_CARTO_API_KEY',
155
156
  amazonApiKey: 'YOUR_AMAZON_LOCATION_API_KEY',
156
157
  awsRegion: 'us-east-1',
157
158
  mapboxAccessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
@@ -354,6 +355,7 @@ API key and load straight from `https://styles.maptoolkit.org/{styleId}.json`:
354
355
  | `panelWidth` | `number` | `340` | Floating panel width in pixels |
355
356
  | `className` | `string` | `''` | Extra class for the control button container |
356
357
  | `mapTilerApiKey` | `string` | `undefined` | Initial MapTiler API key for built-in MapTiler styles |
358
+ | `cartoApiKey` | `string` | `undefined` | Initial CARTO API key for built-in CARTO raster basemaps |
357
359
  | `amazonApiKey` | `string` | `undefined` | Initial Amazon Location API key for built-in Amazon styles |
358
360
  | `awsRegion` | `string` | `'us-east-1'` | AWS region for built-in Amazon Location styles |
359
361
  | `mapboxAccessToken` | `string` | `undefined` | Initial Mapbox access token for built-in Mapbox styles and the Mapbox Traffic overlay |
@@ -424,6 +426,7 @@ const control = new BasemapControl({
424
426
  - `toggleBasemap(id)` - Add the raster basemap if inactive, otherwise remove it
425
427
  - `isBasemapActive(id)` - Whether the basemap is currently active
426
428
  - `setMapTilerApiKey(apiKey)` - Set or update the MapTiler API key used by MapTiler styles
429
+ - `setCartoApiKey(apiKey)` - Set or update the CARTO API key used by CARTO raster basemaps
427
430
  - `setAmazonCredentials(apiKey, awsRegion)` - Set or update Amazon Location credentials
428
431
  - `setMapboxAccessToken(accessToken)` - Set or update the Mapbox access token
429
432
  - `setProtomapsApiKey(apiKey)` - Set or update the Protomaps API key used by Protomaps styles
@@ -662,10 +662,10 @@ const DEFAULT_BASEMAPS = [
662
662
  description: "Light basemap for data overlays.",
663
663
  attribution: CARTO_ATTRIBUTION,
664
664
  tiles: [
665
- "https://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
666
- "https://b.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
667
- "https://c.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
668
- "https://d.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png"
665
+ "https://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png?key={api-key}",
666
+ "https://b.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png?key={api-key}",
667
+ "https://c.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png?key={api-key}",
668
+ "https://d.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png?key={api-key}"
669
669
  ],
670
670
  maxzoom: 20,
671
671
  tags: ["carto", "light", "positron"]
@@ -677,7 +677,7 @@ const DEFAULT_BASEMAPS = [
677
677
  category: "Light",
678
678
  description: "Light basemap without labels.",
679
679
  attribution: CARTO_ATTRIBUTION,
680
- tiles: ["https://a.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png"],
680
+ tiles: ["https://a.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png?key={api-key}"],
681
681
  maxzoom: 20,
682
682
  tags: ["carto", "light", "no labels"]
683
683
  }),
@@ -689,7 +689,7 @@ const DEFAULT_BASEMAPS = [
689
689
  description: "Carto Positron labels layer.",
690
690
  attribution: CARTO_ATTRIBUTION,
691
691
  tiles: [
692
- "https://a.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png"
692
+ "https://a.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png?key={api-key}"
693
693
  ],
694
694
  maxzoom: 20,
695
695
  tags: ["carto", "labels"]
@@ -702,10 +702,10 @@ const DEFAULT_BASEMAPS = [
702
702
  description: "Dark basemap for high-contrast overlays.",
703
703
  attribution: CARTO_ATTRIBUTION,
704
704
  tiles: [
705
- "https://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
706
- "https://b.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
707
- "https://c.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
708
- "https://d.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png"
705
+ "https://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png?key={api-key}",
706
+ "https://b.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png?key={api-key}",
707
+ "https://c.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png?key={api-key}",
708
+ "https://d.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png?key={api-key}"
709
709
  ],
710
710
  maxzoom: 20,
711
711
  tags: ["carto", "dark", "matter"]
@@ -717,7 +717,7 @@ const DEFAULT_BASEMAPS = [
717
717
  category: "Dark",
718
718
  description: "Dark basemap without labels.",
719
719
  attribution: CARTO_ATTRIBUTION,
720
- tiles: ["https://a.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png"],
720
+ tiles: ["https://a.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png?key={api-key}"],
721
721
  maxzoom: 20,
722
722
  tags: ["carto", "dark", "no labels"]
723
723
  }),
@@ -728,7 +728,7 @@ const DEFAULT_BASEMAPS = [
728
728
  category: "Labels",
729
729
  description: "Carto Dark Matter labels layer.",
730
730
  attribution: CARTO_ATTRIBUTION,
731
- tiles: ["https://a.basemaps.cartocdn.com/dark_only_labels/{z}/{x}/{y}.png"],
731
+ tiles: ["https://a.basemaps.cartocdn.com/dark_only_labels/{z}/{x}/{y}.png?key={api-key}"],
732
732
  maxzoom: 20,
733
733
  tags: ["carto", "dark", "labels"]
734
734
  }),
@@ -740,10 +740,10 @@ const DEFAULT_BASEMAPS = [
740
740
  description: "Detailed street basemap with subtle styling.",
741
741
  attribution: CARTO_ATTRIBUTION,
742
742
  tiles: [
743
- "https://a.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png",
744
- "https://b.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png",
745
- "https://c.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png",
746
- "https://d.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png"
743
+ "https://a.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png?key={api-key}",
744
+ "https://b.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png?key={api-key}",
745
+ "https://c.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png?key={api-key}",
746
+ "https://d.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png?key={api-key}"
747
747
  ],
748
748
  maxzoom: 20,
749
749
  tags: ["carto", "street", "voyager"]
@@ -756,7 +756,7 @@ const DEFAULT_BASEMAPS = [
756
756
  description: "Carto Voyager without labels.",
757
757
  attribution: CARTO_ATTRIBUTION,
758
758
  tiles: [
759
- "https://a.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}.png"
759
+ "https://a.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}.png?key={api-key}"
760
760
  ],
761
761
  maxzoom: 20,
762
762
  tags: ["carto", "voyager", "no labels"]
@@ -769,7 +769,7 @@ const DEFAULT_BASEMAPS = [
769
769
  description: "Carto Voyager labels layer.",
770
770
  attribution: CARTO_ATTRIBUTION,
771
771
  tiles: [
772
- "https://a.basemaps.cartocdn.com/rastertiles/voyager_only_labels/{z}/{x}/{y}.png"
772
+ "https://a.basemaps.cartocdn.com/rastertiles/voyager_only_labels/{z}/{x}/{y}.png?key={api-key}"
773
773
  ],
774
774
  maxzoom: 20,
775
775
  tags: ["carto", "voyager", "labels"]
@@ -782,7 +782,7 @@ const DEFAULT_BASEMAPS = [
782
782
  description: "Carto Voyager labels-under variant.",
783
783
  attribution: CARTO_ATTRIBUTION,
784
784
  tiles: [
785
- "https://a.basemaps.cartocdn.com/rastertiles/voyager_labels_under/{z}/{x}/{y}.png"
785
+ "https://a.basemaps.cartocdn.com/rastertiles/voyager_labels_under/{z}/{x}/{y}.png?key={api-key}"
786
786
  ],
787
787
  maxzoom: 20,
788
788
  tags: ["carto", "voyager", "labels"]
@@ -1889,6 +1889,10 @@ const MAPTILER_API_KEY_QUERY = "?key";
1889
1889
  const MAPTILER_API_KEY_EMPTY_QUERY = "?key=";
1890
1890
  const AWS_REGION_PLACEHOLDER = "{aws-region}";
1891
1891
  const PROVIDER_CREDENTIAL_HELP = {
1892
+ carto: {
1893
+ url: "https://carto.com/basemaps/apikey/",
1894
+ label: "Get a CARTO API key"
1895
+ },
1892
1896
  amazon: {
1893
1897
  url: "https://docs.aws.amazon.com/location/latest/developerguide/using-apikeys.html",
1894
1898
  label: "Get an Amazon API key"
@@ -1927,6 +1931,7 @@ const PROVIDER_CREDENTIAL_HELP = {
1927
1931
  }
1928
1932
  };
1929
1933
  const RASTER_KEY_LABELS = {
1934
+ carto: "CARTO API key",
1930
1935
  google: "Google Maps API key",
1931
1936
  here: "HERE API key",
1932
1937
  stadia: "Stadia Maps API key",
@@ -1986,6 +1991,7 @@ class BasemapControl {
1986
1991
  // stacked raster overlay so each can be added or removed independently.
1987
1992
  __publicField(this, "_managedRasters", new globalThis.Map());
1988
1993
  __publicField(this, "_mapTilerApiKey", "");
1994
+ __publicField(this, "_cartoApiKey", "");
1989
1995
  __publicField(this, "_amazonApiKey", "");
1990
1996
  __publicField(this, "_awsRegion", "");
1991
1997
  __publicField(this, "_mapboxAccessToken", "");
@@ -2051,6 +2057,7 @@ class BasemapControl {
2051
2057
  });
2052
2058
  this._options = { ...DEFAULT_OPTIONS, ...options };
2053
2059
  this._mapTilerApiKey = (options == null ? void 0 : options.mapTilerApiKey) ?? "";
2060
+ this._cartoApiKey = (options == null ? void 0 : options.cartoApiKey) ?? "";
2054
2061
  this._amazonApiKey = (options == null ? void 0 : options.amazonApiKey) ?? "";
2055
2062
  this._awsRegion = (options == null ? void 0 : options.awsRegion) ?? "us-east-1";
2056
2063
  this._mapboxAccessToken = (options == null ? void 0 : options.mapboxAccessToken) ?? "";
@@ -2175,6 +2182,12 @@ class BasemapControl {
2175
2182
  this._renderContent();
2176
2183
  this._emit({ type: "statechange", state: this.getState() });
2177
2184
  }
2185
+ setCartoApiKey(apiKey) {
2186
+ this._cartoApiKey = apiKey;
2187
+ this._clearError();
2188
+ this._renderContent();
2189
+ this._emit({ type: "statechange", state: this.getState() });
2190
+ }
2178
2191
  setAmazonCredentials(apiKey, awsRegion = this._awsRegion) {
2179
2192
  this._amazonApiKey = apiKey;
2180
2193
  this._awsRegion = awsRegion;
@@ -2318,12 +2331,16 @@ class BasemapControl {
2318
2331
  let resolvedStyleUrl;
2319
2332
  if (isOverlay) {
2320
2333
  await this._waitForStyleReady();
2334
+ const resolvedRasterTiles = basemap.source.type === "raster" ? await this._resolveRasterTiles(basemap) : void 0;
2335
+ if (basemap.source.type === "vector-overlay") {
2336
+ this._resolveVectorOverlaySource(basemap, basemap.source);
2337
+ }
2321
2338
  if (effectiveMode === "replace") {
2322
2339
  this._removeManagedBasemap();
2323
2340
  } else {
2324
2341
  this._removeManagedRaster(basemap.id);
2325
2342
  }
2326
- managedRaster = await this._addOverlay(basemap);
2343
+ managedRaster = await this._addOverlay(basemap, resolvedRasterTiles);
2327
2344
  } else {
2328
2345
  const styleUrl = this._resolveStyleUrl(basemap);
2329
2346
  resolvedStyleUrl = styleUrl;
@@ -2780,6 +2797,23 @@ class BasemapControl {
2780
2797
  // missing-credential error, so the two never drift (#837).
2781
2798
  _providerFieldDefs() {
2782
2799
  return [
2800
+ {
2801
+ provider: "carto",
2802
+ label: "CARTO",
2803
+ has: this._hasCartoBasemaps(),
2804
+ fields: [
2805
+ {
2806
+ className: "basemap-control-carto-key",
2807
+ type: "password",
2808
+ placeholder: "CARTO API key",
2809
+ ariaLabel: "CARTO API key",
2810
+ value: this._cartoApiKey,
2811
+ onInput: (value) => {
2812
+ this._cartoApiKey = value;
2813
+ }
2814
+ }
2815
+ ]
2816
+ },
2783
2817
  {
2784
2818
  provider: "maptiler",
2785
2819
  label: "MapTiler",
@@ -3121,6 +3155,7 @@ class BasemapControl {
3121
3155
  row.appendChild(
3122
3156
  this._createSelect(
3123
3157
  "Provider",
3158
+ "All providers",
3124
3159
  this._state.providerFilter,
3125
3160
  providers.map((provider) => ({ value: provider.id, label: provider.name })),
3126
3161
  (value) => {
@@ -3134,6 +3169,7 @@ class BasemapControl {
3134
3169
  row.appendChild(
3135
3170
  this._createSelect(
3136
3171
  "Category",
3172
+ "All categories",
3137
3173
  this._state.categoryFilter,
3138
3174
  categories.map((category) => ({ value: category, label: category })),
3139
3175
  (value) => {
@@ -3146,13 +3182,13 @@ class BasemapControl {
3146
3182
  );
3147
3183
  return row;
3148
3184
  }
3149
- _createSelect(label, value, options, onChange) {
3185
+ _createSelect(label, allLabel, value, options, onChange) {
3150
3186
  const select = document.createElement("select");
3151
3187
  select.className = "basemap-control-select";
3152
3188
  select.setAttribute("aria-label", label);
3153
3189
  const all = document.createElement("option");
3154
3190
  all.value = "";
3155
- all.textContent = `All ${label.toLowerCase()}s`;
3191
+ all.textContent = allLabel;
3156
3192
  select.appendChild(all);
3157
3193
  options.forEach((option) => {
3158
3194
  const element = document.createElement("option");
@@ -3281,6 +3317,9 @@ class BasemapControl {
3281
3317
  _hasProtomapsBasemaps() {
3282
3318
  return this._basemaps.some((basemap) => basemap.provider === "protomaps");
3283
3319
  }
3320
+ _hasCartoBasemaps() {
3321
+ return this._basemaps.some((basemap) => basemap.provider === "carto");
3322
+ }
3284
3323
  _hasStadiaBasemaps() {
3285
3324
  return this._basemaps.some((basemap) => basemap.provider === "stadia");
3286
3325
  }
@@ -3303,7 +3342,7 @@ class BasemapControl {
3303
3342
  );
3304
3343
  }
3305
3344
  _hasProviderSettings() {
3306
- return this._hasMapTilerBasemaps() || this._hasAmazonBasemaps() || this._hasMapboxBasemaps() || this._hasProtomapsBasemaps() || this._hasStadiaBasemaps() || this._hasTiandituBasemaps() || this._hasTomTomBasemaps() || this._hasHereBasemaps() || this._hasGoogleApiKeyBasemaps();
3345
+ return this._hasMapTilerBasemaps() || this._hasCartoBasemaps() || this._hasAmazonBasemaps() || this._hasMapboxBasemaps() || this._hasProtomapsBasemaps() || this._hasStadiaBasemaps() || this._hasTiandituBasemaps() || this._hasTomTomBasemaps() || this._hasHereBasemaps() || this._hasGoogleApiKeyBasemaps();
3307
3346
  }
3308
3347
  _resolveStyleUrl(basemap) {
3309
3348
  if (basemap.source.type !== "style" && basemap.source.type !== "vector-style") {
@@ -3450,15 +3489,15 @@ class BasemapControl {
3450
3489
  }
3451
3490
  // Add a stackable overlay (raster tiles or a vector overlay) to the map,
3452
3491
  // resolving any provider credentials first.
3453
- async _addOverlay(basemap) {
3492
+ async _addOverlay(basemap, resolvedRasterTiles) {
3454
3493
  if (basemap.source.type === "vector-overlay") {
3455
3494
  return this._addVectorOverlay(basemap, basemap.source);
3456
3495
  }
3457
- return this._addRasterBasemap(basemap);
3496
+ return this._addRasterBasemap(basemap, resolvedRasterTiles);
3458
3497
  }
3459
- async _addRasterBasemap(basemap) {
3498
+ async _addRasterBasemap(basemap, resolvedTiles) {
3460
3499
  if (!this._map || basemap.source.type !== "raster") return void 0;
3461
- const tiles = await this._resolveRasterTiles(basemap);
3500
+ const tiles = resolvedTiles ?? await this._resolveRasterTiles(basemap);
3462
3501
  const sourceId = `${CONTROL_SOURCE_PREFIX}-${basemap.id}`;
3463
3502
  const layerId = [CONTROL_LAYER_PREFIX, basemap.id].filter(Boolean).join("-");
3464
3503
  const beforeId = this._getBasemapInsertBeforeId();
@@ -3560,6 +3599,7 @@ class BasemapControl {
3560
3599
  return tiles.map((tile) => tile.split(API_KEY_PLACEHOLDER).join(encoded));
3561
3600
  }
3562
3601
  _rasterApiKeyFor(provider) {
3602
+ if (provider === "carto") return this._cartoApiKey.trim();
3563
3603
  if (provider === "tomtom") return this._tomtomApiKey.trim();
3564
3604
  if (provider === "here") return this._hereApiKey.trim();
3565
3605
  if (provider === "stadia") return this._stadiaApiKey.trim();
@@ -3825,4 +3865,4 @@ exports.createBasemapCatalog = createBasemapCatalog;
3825
3865
  exports.filterBasemaps = filterBasemaps;
3826
3866
  exports.getBasemapCategories = getBasemapCategories;
3827
3867
  exports.resolveBasemapProviders = resolveBasemapProviders;
3828
- //# sourceMappingURL=BasemapControl-DgR3c4ci.cjs.map
3868
+ //# sourceMappingURL=BasemapControl-CD6PSvCv.cjs.map