@maptiler/sdk 1.0.6 → 1.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maptiler/sdk",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "The Javascript & TypeScript map SDK tailored for MapTiler Cloud",
5
5
  "module": "dist/maptiler-sdk.mjs",
6
6
  "types": "dist/maptiler-sdk.d.ts",
package/readme.md CHANGED
@@ -50,6 +50,21 @@ const map = new maptilersdk.Map({
50
50
  container: mapContainer,
51
51
  });
52
52
  ```
53
+
54
+ Alternativelly, the `apiKey` can be set as Map option intead of in the `config` object. Yet, this will still internally propagate to the `config` obejct:
55
+ ```ts
56
+ import * as maptilersdk from '@maptiler/sdk';
57
+
58
+ // Let's say you have a DIV ready to receive a map
59
+ const mapContainer = document.getElementById('my-container-div');
60
+
61
+ // Instanciate the map
62
+ const map = new maptilersdk.Map({
63
+ container: mapContainer,
64
+ apiKey: 'YOUR_API_KEY';
65
+ });
66
+ ```
67
+
53
68
  By default, the map will be initialized with the style [streets-v2](https://www.maptiler.com/maps/#style=streets-v2).
54
69
 
55
70
  Depending on the framework and environment your are using for your application, you will have to also include the CSS file.
@@ -150,10 +165,13 @@ Here is the full list:
150
165
  - `MapStyle.STREETS.DARK` (variant)
151
166
  - `MapStyle.STREETS.LIGHT` (variant)
152
167
  - `MapStyle.STREETS.PASTEL` (variant)
153
- - `MapStyle.OUTDOOR` reference style for adventure
154
- - `MapStyle.WINTER` reference style for winter adventure
155
168
  - `MapStyle.SATELLITE` reference style satellite and airborne imagery (no variants)
156
169
  - `MapStyle.HYBRID` reference style satellite and airborne imagery with labels (no variants)
170
+ - `MapStyle.OUTDOOR` reference style for adventure
171
+ - `MapStyle.WINTER` reference style for winter adventure
172
+ - `MapStyle.DATAVIZ`, the perfect style for data visualization, with very little noise
173
+ - `MapStyle.DATAVIZ.DARK` (variant)
174
+ - `MapStyle.DATAVIZ.LIGHT` (variant)
157
175
  - `MapStyle.BASIC` reference style for minimalist design and general purpose
158
176
  - `MapStyle.BASIC.DARK` (variant)
159
177
  - `MapStyle.BASIC.LIGHT` (variant)
@@ -174,9 +192,7 @@ Here is the full list:
174
192
  - `MapStyle.TONER.LITE` (variant)
175
193
  - `MapStyle.TONER.LINES` (variant)
176
194
  - `MapStyle.OPENSTREETMAP` (reference style, this one does not have any variants)
177
- - `MapStyle.DATAVIZ`, the perfect style for data visualization, with very little noise
178
- - `MapStyle.DATAVIZ.DARK` (variant)
179
- - `MapStyle.DATAVIZ.LIGHT` (variant)
195
+
180
196
 
181
197
  All reference styles (instances of `ReferenceMapStyle`) and style variants (instances of `MapStyleVariant`) have methods to know the alternative styles and variant that belong to the same reference style (`.getVariants()`). This is handy to provide a default/dark/light alternative color scheme, yet preserving the same level of details as in the reference style. Read more about about [ReferenceMapStyle](docsmd/classes/ReferenceMapStyle.md) and [MapStyleVariant](docsmd/classes/MapStyleVariant.md).
182
198
  </details>
@@ -320,19 +336,47 @@ map.disableTerrain()
320
336
 
321
337
 
322
338
  # Easy language switching
323
- The language generally depends on the style but we made it possible to easily update it with a single function and from a built-in list of languages:
339
+ The language generally depends on the style but we made it possible to easily set and update from a built-in list of languages.
340
+
341
+ The builtin list of supported languages is accessible from the `Language` object:
342
+ ```ts
343
+ import { Language } from "@maptiler/sdk";
344
+ ```
345
+ In the UMD bundle, it will be directly at `maptilersdk.Language`.
346
+
347
+ There three distinct ways to set the language of a map:
324
348
 
349
+ 1. **Global way, using the config object:**
325
350
  ```ts
326
- map.setLanguage(maptilersdk.Language.ENGLISH);
351
+ import { config } from "@maptiler/sdk";
352
+
353
+ config.primaryLanguage = Language.ENGLISH;
327
354
  ```
355
+ Then, the if any further language is setting is applied, all the map instances created afterward will use this language.
356
+
357
+ 2. **Set the language at instanciation time:**
358
+ ```ts
359
+ const map = new Map({
360
+ // some options...
361
+ language: Language.ENGLISH, // the ISO codes can also be used (eg. "en")
362
+ })
363
+ ```
364
+ It will only apply `ENGLISH` as the language of this specific map instance (and will not alter the global `config`).
365
+
366
+ 3. **Set the language after the map has been instanciated:**
367
+ ```ts
368
+ map.setLanguage(Language.ENGLISH);
369
+ ```
370
+ Again, it will only apply `ENGLISH` as the language of this specific map instance (and will not alter the global `config`).
371
+
328
372
 
329
373
  The list of supported languages is built-in and can be found [here](src/language.ts). In addition, there are spacial language *flags*:
330
- - `maptilersdk.Language.AUTO` **[DEFAULT]** uses the language defined in the web browser
331
- - `maptilersdk.Language.LOCAL` uses the language local to each country
332
- - `maptilersdk.Language.LATIN` uses a default with latin characters
333
- - `maptilersdk.Language.NON_LATIN` uses a default with non-latin characters
374
+ - `Language.AUTO` **[DEFAULT]** uses the language defined in the web browser
375
+ - `Language.LOCAL` uses the language local to each country
376
+ - `Language.LATIN` uses a default with latin characters
377
+ - `Language.NON_LATIN` uses a default with non-latin characters
334
378
 
335
- Whenever a label is not supported in the defined language, it falls back to `maptilersdk.Language.LATIN`.
379
+ Whenever a label is not supported in the defined language, it falls back to `Language.LATIN`.
336
380
 
337
381
  Here is a sample of some compatible languages:
338
382
  ![](images/screenshots/multilang.gif)
package/src/Map.ts CHANGED
@@ -8,7 +8,7 @@ import type {
8
8
  } from "maplibre-gl";
9
9
  import { v4 as uuidv4 } from "uuid";
10
10
  import { ReferenceMapStyle, MapStyleVariant } from "@maptiler/client";
11
- import { config } from "./config";
11
+ import { config, SdkConfig } from "./config";
12
12
  import { defaults } from "./defaults";
13
13
  import { MaptilerLogoControl } from "./MaptilerLogoControl";
14
14
  import { enableRTL } from "./tools";
@@ -57,6 +57,19 @@ export type MapOptions = Omit<MapOptionsML, "style" | "maplibreLogo"> & {
57
57
  */
58
58
  style?: ReferenceMapStyle | MapStyleVariant | StyleSpecification | string;
59
59
 
60
+ /**
61
+ * Define the language of the map. This can be done directly with a language ISO code (eg. "en")
62
+ * or with a built-in shorthand (eg. Language.ENGLISH).
63
+ * Note that this is equivalent to setting the `config.primaryLanguage` and will overwrite it.
64
+ */
65
+ language?: LanguageString;
66
+
67
+ /**
68
+ * Define the MapTiler Cloud API key to be used. This is strictly equivalent to setting
69
+ * `config.apiKey` and will overwrite it.
70
+ */
71
+ apiKey?: string;
72
+
60
73
  /**
61
74
  * Shows the MapTiler logo if `true`. Note that the logo is always displayed on free plan.
62
75
  */
@@ -127,8 +140,18 @@ export class Map extends maplibregl.Map {
127
140
  private isStyleInitialized = false;
128
141
  private isTerrainEnabled = false;
129
142
  private terrainExaggeration = 1;
143
+ private primaryLanguage: LanguageString | null = null;
144
+ private secondaryLanguage: LanguageString | null = null;
130
145
 
131
146
  constructor(options: MapOptions) {
147
+ // if (options.language) {
148
+ // config.primaryLanguage = options.language;
149
+ // }
150
+
151
+ if (options.apiKey) {
152
+ config.apiKey = options.apiKey;
153
+ }
154
+
132
155
  const style = styleToStyle(options.style);
133
156
  const hashPreConstructor = location.hash;
134
157
 
@@ -176,6 +199,9 @@ export class Map extends maplibregl.Map {
176
199
  },
177
200
  });
178
201
 
202
+ this.primaryLanguage = options.language ?? config.primaryLanguage;
203
+ this.secondaryLanguage = config.secondaryLanguage;
204
+
179
205
  // Map centering and geolocation
180
206
  this.once("styledata", async () => {
181
207
  // Not using geolocation centering if...
@@ -250,30 +276,10 @@ export class Map extends maplibregl.Map {
250
276
  }
251
277
  });
252
278
 
253
- // Check if language has been modified and. If so, it will be updated during the next lifecycle step
254
- this.on("styledataloading", () => {
255
- this.languageShouldUpdate =
256
- !!config.primaryLanguage || !!config.secondaryLanguage;
257
- });
258
-
259
279
  // If the config includes language changing, we must update the map language
260
280
  this.on("styledata", () => {
261
- if (
262
- config.primaryLanguage &&
263
- (this.languageShouldUpdate || !this.isStyleInitialized)
264
- ) {
265
- this.setPrimaryLanguage(config.primaryLanguage);
266
- }
267
-
268
- if (
269
- config.secondaryLanguage &&
270
- (this.languageShouldUpdate || !this.isStyleInitialized)
271
- ) {
272
- this.setSecondaryLanguage(config.secondaryLanguage);
273
- }
274
-
275
- this.languageShouldUpdate = false;
276
- this.isStyleInitialized = true;
281
+ this.setPrimaryLanguage(this.primaryLanguage);
282
+ this.setSecondaryLanguage(this.secondaryLanguage);
277
283
  });
278
284
 
279
285
  // this even is in charge of reaplying the terrain elevation after the
@@ -464,14 +470,13 @@ export class Map extends maplibregl.Map {
464
470
  return;
465
471
  }
466
472
 
473
+ this.primaryLanguage = language;
474
+
467
475
  this.onStyleReady(() => {
468
476
  if (language === Language.AUTO) {
469
477
  return this.setPrimaryLanguage(getBrowserLanguage());
470
478
  }
471
479
 
472
- // We want to keep track of it to apply the language again when changing the style
473
- config.primaryLanguage = language;
474
-
475
480
  const layers = this.getStyle().layers;
476
481
 
477
482
  // detects pattern like "{name:somelanguage}" with loose spacing
@@ -621,7 +626,7 @@ export class Map extends maplibregl.Map {
621
626
  }
622
627
 
623
628
  /**
624
- * Define the secondary language of the map.
629
+ * Define the secondary language of the map. Note that this is not supported by all the map styles
625
630
  * Note that most styles do not allow a secondary language and this function only works if the style allows (no force adding)
626
631
  * @param language
627
632
  */
@@ -630,14 +635,13 @@ export class Map extends maplibregl.Map {
630
635
  return;
631
636
  }
632
637
 
638
+ this.secondaryLanguage = language;
639
+
633
640
  this.onStyleReady(() => {
634
641
  if (language === Language.AUTO) {
635
642
  return this.setSecondaryLanguage(getBrowserLanguage());
636
643
  }
637
644
 
638
- // We want to keep track of it to apply the language again when changing the style
639
- config.secondaryLanguage = language;
640
-
641
645
  const layers = this.getStyle().layers;
642
646
 
643
647
  // detects pattern like "{name:somelanguage}" with loose spacing
@@ -755,6 +759,22 @@ export class Map extends maplibregl.Map {
755
759
  });
756
760
  }
757
761
 
762
+ /**
763
+ * Get the primary language
764
+ * @returns
765
+ */
766
+ getPrimaryLanguage(): LanguageString {
767
+ return this.primaryLanguage;
768
+ }
769
+
770
+ /**
771
+ * Get the secondary language
772
+ * @returns
773
+ */
774
+ getSecondaryLanguage(): LanguageString {
775
+ return this.secondaryLanguage;
776
+ }
777
+
758
778
  /**
759
779
  * Get the exaggeration factor applied to the terrain
760
780
  * @returns
@@ -904,4 +924,23 @@ export class Map extends maplibregl.Map {
904
924
  hashBin[4] = this.getBearing();
905
925
  return Base64.fromUint8Array(new Uint8Array(hashBin.buffer));
906
926
  }
927
+
928
+ /**
929
+ * Get the SDK config object.
930
+ * This is convenient to dispatch the SDK configuration to externally built layers
931
+ * that do not directly have access to the SDK configuration but do have access to a Map instance.
932
+ * @returns
933
+ */
934
+ getSdkConfig(): SdkConfig {
935
+ return config;
936
+ }
937
+
938
+ /**
939
+ * Get the MapTiler session ID. Convenient to dispatch to externaly built component
940
+ * that do not directly have access to the SDK configuration but do have access to a Map instance.
941
+ * @returns
942
+ */
943
+ getMaptilerSessionId(): string {
944
+ return MAPTILER_SESSION_ID;
945
+ }
907
946
  }