@maptiler/sdk 1.0.5 → 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.5",
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,9 +8,9 @@ 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
- import { CustomLogoControl } from "./CustomLogoControl";
13
+ import { MaptilerLogoControl } from "./MaptilerLogoControl";
14
14
  import { enableRTL } from "./tools";
15
15
  import {
16
16
  getBrowserLanguage,
@@ -19,10 +19,10 @@ import {
19
19
  LanguageString,
20
20
  } from "./language";
21
21
  import { styleToStyle } from "./mapstyle";
22
- import { TerrainControl } from "./TerrainControl";
22
+ import { MaptilerTerrainControl } from "./MaptilerTerrainControl";
23
23
  import { MaptilerNavigationControl } from "./MaptilerNavigationControl";
24
24
  import { geolocation } from "@maptiler/client";
25
- import { CustomGeolocateControl } from "./CustomGeolocateControl";
25
+ import { MaptilerGeolocateControl } from "./MaptilerGeolocateControl";
26
26
 
27
27
  // StyleSwapOptions is not exported by Maplibre, but we can redefine it (used for setStyle)
28
28
  export type TransformStyleFunction = (
@@ -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
@@ -323,7 +354,7 @@ export class Map extends maplibregl.Map {
323
354
  const logoURL: string = tileJsonContent.logo;
324
355
 
325
356
  this.addControl(
326
- new CustomLogoControl({ logoURL }),
357
+ new MaptilerLogoControl({ logoURL }),
327
358
  options.logoPosition
328
359
  );
329
360
 
@@ -332,7 +363,7 @@ export class Map extends maplibregl.Map {
332
363
  this.addControl(new maplibregl.AttributionControl(options));
333
364
  }
334
365
  } else if (options.maptilerLogo) {
335
- this.addControl(new CustomLogoControl(), options.logoPosition);
366
+ this.addControl(new MaptilerLogoControl(), options.logoPosition);
336
367
  }
337
368
 
338
369
  // the other controls at init time but be after
@@ -376,7 +407,7 @@ export class Map extends maplibregl.Map {
376
407
 
377
408
  this.addControl(
378
409
  // new maplibregl.GeolocateControl({
379
- new CustomGeolocateControl({
410
+ new MaptilerGeolocateControl({
380
411
  positionOptions: {
381
412
  enableHighAccuracy: true,
382
413
  maximumAge: 0,
@@ -401,7 +432,7 @@ export class Map extends maplibregl.Map {
401
432
  ? "top-right"
402
433
  : options.terrainControl
403
434
  ) as ControlPosition;
404
- this.addControl(new TerrainControl(), position);
435
+ this.addControl(new MaptilerTerrainControl(), position);
405
436
  }
406
437
 
407
438
  // By default, no fullscreen control
@@ -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
  }
@@ -6,7 +6,13 @@ const GeolocateControl = maplibregl.GeolocateControl;
6
6
  const Marker = maplibregl.Marker;
7
7
  const LngLat = maplibregl.LngLat;
8
8
 
9
- export class CustomGeolocateControl extends GeolocateControl {
9
+ /**
10
+ * The MaptilerGeolocateControl is an extension of the original GeolocateControl
11
+ * with a few changes. In this version, the active mode persists as long as the
12
+ * location is still centered. This means it's robust to rotation, pitch and zoom.
13
+ *
14
+ */
15
+ export class MaptilerGeolocateControl extends GeolocateControl {
10
16
  private lastUpdatedCenter = new LngLat(0, 0);
11
17
 
12
18
  /**
@@ -1,8 +1,9 @@
1
1
  import maplibregl from "maplibre-gl";
2
+ import type { LogoOptions as LogoOptionsML } from "maplibre-gl";
2
3
  import { defaults } from "./defaults";
3
4
  import { Map } from "./Map";
4
5
 
5
- type LogoOptions = maplibregl.LogoOptions & {
6
+ type LogoOptions = LogoOptionsML & {
6
7
  logoURL?: string;
7
8
  linkURL?: string;
8
9
  };
@@ -11,7 +12,7 @@ type LogoOptions = maplibregl.LogoOptions & {
11
12
  * This LogoControl extends the MapLibre LogoControl but instead can use any image URL and
12
13
  * any link URL. By default this is using MapTiler logo and URL.
13
14
  */
14
- export class CustomLogoControl extends maplibregl.LogoControl {
15
+ export class MaptilerLogoControl extends maplibregl.LogoControl {
15
16
  private logoURL = "";
16
17
  private linkURL = "";
17
18
 
@@ -4,20 +4,10 @@ import { Map } from "./Map";
4
4
  import maplibregl from "maplibre-gl";
5
5
 
6
6
  /**
7
- * An `TerrainControl` control adds a button to turn terrain on and off.
8
- *
9
- * @implements {IControl}
10
- * @param {Object} [options]
11
- * @param {string} [options.id] The ID of the raster-dem source to use.
12
- * @param {Object} [options.options]
13
- * @param {number} [options.options.exaggeration]
14
- * @example
15
- * var map = new maplibregl.Map({TerrainControl: false})
16
- * .addControl(new maplibregl.TerrainControl({
17
- * source: "terrain"
18
- * }));
7
+ * A `MaptilerTerrainControl` control adds a button to turn terrain on and off
8
+ * by triggering the terrain logic that is already deployed in the Map object.
19
9
  */
20
- export class TerrainControl implements maplibregl.IControl {
10
+ export class MaptilerTerrainControl implements maplibregl.IControl {
21
11
  _map: Map;
22
12
  _container: HTMLElement;
23
13
  _terrainButton: HTMLButtonElement;
package/src/index.ts CHANGED
@@ -97,6 +97,10 @@ export {
97
97
  import { Map, GeolocationType } from "./Map";
98
98
  import type { MapOptions } from "./Map";
99
99
 
100
+ import { MaptilerGeolocateControl } from "./MaptilerGeolocateControl";
101
+ import { MaptilerLogoControl } from "./MaptilerLogoControl";
102
+ import { MaptilerTerrainControl } from "./MaptilerTerrainControl";
103
+
100
104
  // importing client functions to expose them as part of the SDK
101
105
  import type {
102
106
  BBox,
@@ -168,4 +172,7 @@ export {
168
172
  Point,
169
173
  ReferenceMapStyle,
170
174
  MapStyleVariant,
175
+ MaptilerGeolocateControl,
176
+ MaptilerLogoControl,
177
+ MaptilerTerrainControl,
171
178
  };