@maptiler/sdk 1.0.6 → 1.0.7

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.7",
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.
@@ -320,19 +335,47 @@ map.disableTerrain()
320
335
 
321
336
 
322
337
  # 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:
338
+ The language generally depends on the style but we made it possible to easily set and update from a built-in list of languages.
339
+
340
+ The builtin list of supported languages is accessible from the `Language` object:
341
+ ```ts
342
+ import { Language } from "@maptiler/sdk";
343
+ ```
344
+ In the UMD bundle, it will be directly at `maptilersdk.Language`.
345
+
346
+ There three distinct ways to set the language of a map:
347
+
348
+ 1. **Global way, using the config object:**
349
+ ```ts
350
+ import { config } from "@maptiler/sdk";
351
+
352
+ config.primaryLanguage = Language.ENGLISH;
353
+ ```
354
+ Then, the if any further language is setting is applied, all the map instances created afterward will use this language.
324
355
 
356
+ 2. **Set the language at instanciation time:**
325
357
  ```ts
326
- map.setLanguage(maptilersdk.Language.ENGLISH);
358
+ const map = new Map({
359
+ // some options...
360
+ language: Language.ENGLISH, // the ISO codes can also be used (eg. "en")
361
+ })
327
362
  ```
363
+ It will only apply `ENGLISH` as the language of this specific map instance (and will not alter the global `config`).
364
+
365
+ 3. **Set the language after the map has been instanciated:**
366
+ ```ts
367
+ map.setLanguage(Language.ENGLISH);
368
+ ```
369
+ Again, it will only apply `ENGLISH` as the language of this specific map instance (and will not alter the global `config`).
370
+
328
371
 
329
372
  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
373
+ - `Language.AUTO` **[DEFAULT]** uses the language defined in the web browser
374
+ - `Language.LOCAL` uses the language local to each country
375
+ - `Language.LATIN` uses a default with latin characters
376
+ - `Language.NON_LATIN` uses a default with non-latin characters
334
377
 
335
- Whenever a label is not supported in the defined language, it falls back to `maptilersdk.Language.LATIN`.
378
+ Whenever a label is not supported in the defined language, it falls back to `Language.LATIN`.
336
379
 
337
380
  Here is a sample of some compatible languages:
338
381
  ![](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
  */
@@ -129,6 +142,14 @@ export class Map extends maplibregl.Map {
129
142
  private terrainExaggeration = 1;
130
143
 
131
144
  constructor(options: MapOptions) {
145
+ // if (options.language) {
146
+ // config.primaryLanguage = options.language;
147
+ // }
148
+
149
+ if (options.apiKey) {
150
+ config.apiKey = options.apiKey;
151
+ }
152
+
132
153
  const style = styleToStyle(options.style);
133
154
  const hashPreConstructor = location.hash;
134
155
 
@@ -256,9 +277,18 @@ export class Map extends maplibregl.Map {
256
277
  !!config.primaryLanguage || !!config.secondaryLanguage;
257
278
  });
258
279
 
280
+ // To flag if the language was already initialized at build time
281
+ // so that the language optionnaly passed in constructor is
282
+ // considered only once and at instanciation time.
283
+ let initLanguageFromConstructor = true;
284
+
259
285
  // If the config includes language changing, we must update the map language
260
286
  this.on("styledata", () => {
261
- if (
287
+ // If the language is set as a constructor options,
288
+ // This prevails on the language from the config.
289
+ if (options.language && initLanguageFromConstructor) {
290
+ this.setPrimaryLanguage(options.language);
291
+ } else if (
262
292
  config.primaryLanguage &&
263
293
  (this.languageShouldUpdate || !this.isStyleInitialized)
264
294
  ) {
@@ -274,6 +304,7 @@ export class Map extends maplibregl.Map {
274
304
 
275
305
  this.languageShouldUpdate = false;
276
306
  this.isStyleInitialized = true;
307
+ initLanguageFromConstructor = false;
277
308
  });
278
309
 
279
310
  // this even is in charge of reaplying the terrain elevation after the
@@ -469,9 +500,6 @@ export class Map extends maplibregl.Map {
469
500
  return this.setPrimaryLanguage(getBrowserLanguage());
470
501
  }
471
502
 
472
- // We want to keep track of it to apply the language again when changing the style
473
- config.primaryLanguage = language;
474
-
475
503
  const layers = this.getStyle().layers;
476
504
 
477
505
  // detects pattern like "{name:somelanguage}" with loose spacing
@@ -635,9 +663,6 @@ export class Map extends maplibregl.Map {
635
663
  return this.setSecondaryLanguage(getBrowserLanguage());
636
664
  }
637
665
 
638
- // We want to keep track of it to apply the language again when changing the style
639
- config.secondaryLanguage = language;
640
-
641
666
  const layers = this.getStyle().layers;
642
667
 
643
668
  // detects pattern like "{name:somelanguage}" with loose spacing
@@ -904,4 +929,23 @@ export class Map extends maplibregl.Map {
904
929
  hashBin[4] = this.getBearing();
905
930
  return Base64.fromUint8Array(new Uint8Array(hashBin.buffer));
906
931
  }
932
+
933
+ /**
934
+ * Get the SDK config object.
935
+ * This is convenient to dispatch the SDK configuration to externally built layers
936
+ * that do not directly have access to the SDK configuration but do have access to a Map instance.
937
+ * @returns
938
+ */
939
+ getSdkConfig(): SdkConfig {
940
+ return config;
941
+ }
942
+
943
+ /**
944
+ * Get the MapTiler session ID. Convenient to dispatch to externaly built component
945
+ * that do not directly have access to the SDK configuration but do have access to a Map instance.
946
+ * @returns
947
+ */
948
+ getMaptilerSessionId(): string {
949
+ return MAPTILER_SESSION_ID;
950
+ }
907
951
  }