@maptiler/sdk 2.4.0 → 2.4.2-rc.1
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/dist/maptiler-sdk.mjs +1506 -1402
- package/dist/maptiler-sdk.mjs.map +1 -1
- package/dist/src/Map.d.ts +5 -2
- package/dist/src/tools.d.ts +40 -1
- package/package.json +1 -1
- package/readme.md +44 -20
- package/tsconfig.json +2 -2
package/dist/src/Map.d.ts
CHANGED
|
@@ -28,11 +28,12 @@ export type MapOptions = Omit<MapOptionsML, "style" | "maplibreLogo"> & {
|
|
|
28
28
|
*/
|
|
29
29
|
style?: ReferenceMapStyle | MapStyleVariant | StyleSpecification | string;
|
|
30
30
|
/**
|
|
31
|
-
* Define the language of the map. This can be done directly with a language ISO code (eg. "en")
|
|
31
|
+
* Define the language of the map. This can be done directly with a language ISO code (eg. "en"),
|
|
32
|
+
* the ISO code prepended with the OSM flag (eg. "name:en" or even just "name"),
|
|
32
33
|
* or with a built-in shorthand (eg. Language.ENGLISH).
|
|
33
34
|
* Note that this is equivalent to setting the `config.primaryLanguage` and will overwrite it.
|
|
34
35
|
*/
|
|
35
|
-
language?: LanguageInfo;
|
|
36
|
+
language?: LanguageInfo | string;
|
|
36
37
|
/**
|
|
37
38
|
* Define the MapTiler Cloud API key to be used. This is strictly equivalent to setting
|
|
38
39
|
* `config.apiKey` and will overwrite it.
|
|
@@ -127,6 +128,8 @@ export declare class Map extends maplibregl.Map {
|
|
|
127
128
|
private terrainAnimationDuration;
|
|
128
129
|
private monitoredStyleUrls;
|
|
129
130
|
private styleInProcess;
|
|
131
|
+
private originalLabelStyle;
|
|
132
|
+
private isStyleLocalized;
|
|
130
133
|
constructor(options: MapOptions);
|
|
131
134
|
/**
|
|
132
135
|
* Set the duration (millisec) of the terrain animation for growing or flattening.
|
package/dist/src/tools.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { RequestParameters, ResourceType, RequestTransformFunction } from 'maplibre-gl';
|
|
1
|
+
import { default as maplibregl, RequestParameters, ResourceType, RequestTransformFunction } from 'maplibre-gl';
|
|
2
|
+
import { Map as MapSDK } from './Map';
|
|
2
3
|
|
|
3
4
|
export declare function enableRTL(): void;
|
|
4
5
|
export declare function bindAll(fns: Array<string>, context: any): void;
|
|
@@ -48,3 +49,41 @@ export declare function displayNoWebGlWarning(container: HTMLElement | string):
|
|
|
48
49
|
* Display an error message in the Map div if WebGL2 is not supported
|
|
49
50
|
*/
|
|
50
51
|
export declare function displayWebGLContextLostWarning(container: HTMLElement | string): void;
|
|
52
|
+
/**
|
|
53
|
+
* In a text-field style property (as an object, not a string) the languages that are specified as
|
|
54
|
+
* ["get", "name:XX"] are replaced by the proved replacer (also an object).
|
|
55
|
+
* This replacement happened regardless of how deep in the object the flag is.
|
|
56
|
+
* Note that it does not replace the occurences of ["get", "name"] (local names)
|
|
57
|
+
*/
|
|
58
|
+
export declare function changeFirstLanguage(origExpr: maplibregl.ExpressionSpecification, replacer: maplibregl.ExpressionSpecification, localized: boolean): maplibregl.ExpressionSpecification;
|
|
59
|
+
/**
|
|
60
|
+
* If `localized` is `true`, it checks for the pattern "{name:xx}" (with "xx" being a language iso string).
|
|
61
|
+
* If `localized` is `false`, it check for {name}.
|
|
62
|
+
* In a exact way or is a loose way (such as "foo {name:xx}" or "foo {name} bar")
|
|
63
|
+
*/
|
|
64
|
+
export declare function checkNamePattern(str: string, localized: boolean): {
|
|
65
|
+
contains: boolean;
|
|
66
|
+
exactMatch: boolean;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Replaces the occurences of {name:xx} in a string by a provided object-expression to return a concat object expression
|
|
70
|
+
*/
|
|
71
|
+
export declare function replaceLanguage(origLang: string, newLang: maplibregl.ExpressionSpecification, localized: boolean): maplibregl.ExpressionSpecification;
|
|
72
|
+
/**
|
|
73
|
+
* Find languages used in string label definition.
|
|
74
|
+
* The returned array contains languages such as "en", "fr" but
|
|
75
|
+
* can also contain null that stand for the use of {name}
|
|
76
|
+
*/
|
|
77
|
+
export declare function findLanguageStr(str: string): Array<string | null>;
|
|
78
|
+
/**
|
|
79
|
+
* Find languages used in object label definition.
|
|
80
|
+
* The returned array contains languages such as "en", "fr" but
|
|
81
|
+
* can also contain null that stand for the use of {name}
|
|
82
|
+
*/
|
|
83
|
+
export declare function findLanguageObj(origExpr: maplibregl.ExpressionSpecification): Array<string | null>;
|
|
84
|
+
export declare function computeLabelsLocalizationMetrics(layers: maplibregl.LayerSpecification[], map: MapSDK): {
|
|
85
|
+
unlocalized: number;
|
|
86
|
+
localized: {
|
|
87
|
+
[k: string]: number;
|
|
88
|
+
};
|
|
89
|
+
};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -47,7 +47,7 @@ maptilersdk.config.apiKey = 'YOUR_API_KEY';
|
|
|
47
47
|
// Let's say you have a DIV ready to receive a map
|
|
48
48
|
const mapContainer = document.getElementById('my-container-div');
|
|
49
49
|
|
|
50
|
-
//
|
|
50
|
+
// Instantiate the map
|
|
51
51
|
const map = new maptilersdk.Map({
|
|
52
52
|
container: mapContainer,
|
|
53
53
|
});
|
|
@@ -60,16 +60,16 @@ import * as maptilersdk from '@maptiler/sdk';
|
|
|
60
60
|
// Let's say you have a DIV ready to receive a map
|
|
61
61
|
const mapContainer = document.getElementById('my-container-div');
|
|
62
62
|
|
|
63
|
-
//
|
|
63
|
+
// Instantiate the map
|
|
64
64
|
const map = new maptilersdk.Map({
|
|
65
65
|
container: mapContainer,
|
|
66
|
-
apiKey: 'YOUR_API_KEY'
|
|
66
|
+
apiKey: 'YOUR_API_KEY'
|
|
67
67
|
});
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
By default, the map will be initialized with the style [streets-v2](https://www.maptiler.com/maps/#style=streets-v2).
|
|
71
71
|
|
|
72
|
-
Depending on the framework and environment you are using for your application, you will have to also include the CSS file.
|
|
72
|
+
Depending on the framework and environment you are using for your application, you will have to also include the CSS file.
|
|
73
73
|
|
|
74
74
|
For example, with a [NextJS](https://nextjs.org/) app, this can take place at the top of the file `_app.ts/js`:
|
|
75
75
|
```ts
|
|
@@ -79,7 +79,7 @@ import "@maptiler/sdk/dist/maptiler-sdk.css";
|
|
|
79
79
|
### TypeScript
|
|
80
80
|
The SDK is fully typed, but it may happen that types defined in Maplibre GL JS are not visible in your project. This is a known issue that comes from Maplibre being a CommonJS bundle.
|
|
81
81
|
|
|
82
|
-
There are mainly two ways to
|
|
82
|
+
There are mainly two ways to address this issue and access to the complete type definition.
|
|
83
83
|
|
|
84
84
|
1. **With `esModuleInterop`**
|
|
85
85
|
|
|
@@ -237,7 +237,7 @@ All reference styles (instances of `ReferenceMapStyle`) and style variants (inst
|
|
|
237
237
|
___
|
|
238
238
|
|
|
239
239
|
|
|
240
|
-
|
|
240
|
+
You can also use classic styles with just a *string* if you know their MapTiler Cloud ID:
|
|
241
241
|
|
|
242
242
|
```ts
|
|
243
243
|
map.setStyle('outdoor-v2');
|
|
@@ -290,7 +290,7 @@ The `geolocation` options will not be taken into consideration in the following
|
|
|
290
290
|
|
|
291
291
|
> 📣 *__Note:__* if none of the options `center` or `hash` is provided to the `Map` constructor, then the map will be centered using the `POINT` strategy, unless the `geolocate` has the value `false`.
|
|
292
292
|
|
|
293
|
-
> 📣 *__Note 2:__* the term *IP geolocation* refers to finding the physical location of a computer using its *IP address*. The *IP address* is a numerical identifier of a computer within a network, just like the phone number for a telephone. The *IP geolocation* is **not** using the GPS of a device and usually provides a precision in the order of a few hundred meters. This precision may vary based on many local parameters such as the density of the network grid or the terrain, this is why it is
|
|
293
|
+
> 📣 *__Note 2:__* the term *IP geolocation* refers to finding the physical location of a computer using its *IP address*. The *IP address* is a numerical identifier of a computer within a network, just like the phone number for a telephone. The *IP geolocation* is **not** using the GPS of a device and usually provides a precision in the order of a few hundred meters. This precision may vary based on many local parameters such as the density of the network grid or the terrain, this is why it is generally better not to use a zoom level higher than `14`.
|
|
294
294
|
|
|
295
295
|
# Easy to add controls
|
|
296
296
|
The term "control" is commonly used for all sorts of buttons and information displays that take place in one of the corners of the map area. The most well-known are probably the `[+]` and `[-]` zoom buttons as well as the attribution information. Plenty of others are possible and we have made a few easy to add and directly accessible from the `Map` constructor options:
|
|
@@ -367,7 +367,7 @@ Or simply disable it:
|
|
|
367
367
|
map.disableTerrain()
|
|
368
368
|
```
|
|
369
369
|
|
|
370
|
-
> 📣 *__Note:__* Keep in mind that setting an exaggeration factor at `0` will result in
|
|
370
|
+
> 📣 *__Note:__* Keep in mind that setting an exaggeration factor at `0` will result in the same result as disabling the elevation but that terrain RGB tiles will still be fetched in the background.
|
|
371
371
|
|
|
372
372
|
> 📣 *__Note 2:__* please be aware that due to the volume and elevation of the map floor in 3D space, the navigation with the terrain enabled is slightly different than without.
|
|
373
373
|
|
|
@@ -389,7 +389,7 @@ map.on("terrain", (e) => {
|
|
|
389
389
|
})
|
|
390
390
|
```
|
|
391
391
|
|
|
392
|
-
Since MapTiler SDK adds animation and the terrain data is necessary all along, the `"terrain"` event will be called at the very
|
|
392
|
+
Since MapTiler SDK adds animation and the terrain data is necessary all along, the `"terrain"` event will be called at the very beginning of the terrain animation when enabling and at the very end when disabling.
|
|
393
393
|
|
|
394
394
|
- `"terrainAnimationStart"` and `"terrainAnimationStop"` events
|
|
395
395
|
|
|
@@ -405,7 +405,7 @@ map.on("terrainAnimationStop", (event) => {
|
|
|
405
405
|
});
|
|
406
406
|
```
|
|
407
407
|
|
|
408
|
-
The `event` argument is an object that contains (
|
|
408
|
+
The `event` argument is an object that contains (among other things) a `terrain` attribute. In the case of `"terrainAnimationStop"`, this terrain attribute is `null` if the animation was about disabling the terrain, otherwise, this is just a propagation of `map.terrain`.
|
|
409
409
|
|
|
410
410
|
In the following example, we decide to associate the terrain animation with a change of camera, e.g. from clicking on the terrain control:
|
|
411
411
|
- when the terrain is enabled, it pops up with an animation and only **then** the camera is animated to take a lower point of view
|
|
@@ -468,7 +468,7 @@ Whenever a label is not supported in the defined language, it falls back to `Lan
|
|
|
468
468
|
Here is a sample of some compatible languages:
|
|
469
469
|

|
|
470
470
|
|
|
471
|
-
|
|
471
|
+
## Built-in support for right-to-left languages
|
|
472
472
|
Languages that are written right-to-left such as Arabic and Hebrew are fully supported by default. No need to install any plugins!
|
|
473
473
|
|
|
474
474
|
<p align="center">
|
|
@@ -476,6 +476,31 @@ Languages that are written right-to-left such as Arabic and Hebrew are fully sup
|
|
|
476
476
|
<img src="images/screenshots/lang-hebrew.jpeg" width="48%"></img>
|
|
477
477
|
</p>
|
|
478
478
|
|
|
479
|
+
## Visitor language modes
|
|
480
|
+
The *visitor* language modes are special built-in modes made to display labels in two different languages, concatenated when available:
|
|
481
|
+
- `Language.VISITOR` concatenates labels in the language of your system and the *local* language
|
|
482
|
+
- `Language.VISITOR_ENGLISH` concatenates labels in English and the *local* language
|
|
483
|
+
|
|
484
|
+
```ts
|
|
485
|
+
const map = new Map({
|
|
486
|
+
// some options...
|
|
487
|
+
language: Language.VISITOR,
|
|
488
|
+
})
|
|
489
|
+
|
|
490
|
+
// or
|
|
491
|
+
|
|
492
|
+
const map = new Map({
|
|
493
|
+
// some options...
|
|
494
|
+
language: Language.VISITOR_ENGLISH,
|
|
495
|
+
})
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
We believe these two modes can be very handy to help the end users identify places, especially when the local labels are not using a latin charset. Here is how it looks like:
|
|
499
|
+
|
|
500
|
+

|
|
501
|
+

|
|
502
|
+
|
|
503
|
+
|
|
479
504
|
# Custom Events and Map Lifecycle
|
|
480
505
|
## Events
|
|
481
506
|
### The `ready` event
|
|
@@ -516,7 +541,7 @@ function init() {
|
|
|
516
541
|
});
|
|
517
542
|
|
|
518
543
|
// We wait for the event.
|
|
519
|
-
// Once triggered, the callback is
|
|
544
|
+
// Once triggered, the callback is ran in its own scope.
|
|
520
545
|
map.on("load", (evt) => {
|
|
521
546
|
// Adding a data source
|
|
522
547
|
map.addSource('my-gps-track-source', {
|
|
@@ -608,7 +633,7 @@ function init() {
|
|
|
608
633
|
});
|
|
609
634
|
|
|
610
635
|
// We wait for the event.
|
|
611
|
-
// Once triggered, the callback is
|
|
636
|
+
// Once triggered, the callback is ran in its own scope.
|
|
612
637
|
map.on("ready", (evt) => {
|
|
613
638
|
// Adding a data source
|
|
614
639
|
map.addSource('my-gps-track-source', {
|
|
@@ -646,9 +671,9 @@ We believe that the *promise* approach is better because it does not nest scopes
|
|
|
646
671
|
> 📣 *__Note:__* Generally speaking, *promises* are not a go to replacement for all event+callback and are suitable only for events that are called only once in the lifecycle of a Map instance. This is the reason why we have decided to provide a *promise* equivalent only for the `load`, `ready` and `loadWithTerrain` events but not for events that may be called multiple time such as interaction events.
|
|
647
672
|
|
|
648
673
|
### The `webglContextLost` event
|
|
649
|
-
The
|
|
674
|
+
The map is rendered with WebGL, that leverages the GPU to provide high-performance graphics. In some cases, the host machine, operating system or the graphics driver, can decide that continuing to run such high performance graphics is unsustainable, and will abort the process. This is called a "WebGL context loss". Such situation happens when the resources are running low or when multiple browser tabs are competing to access graphics memory.
|
|
650
675
|
|
|
651
|
-
The best course of action in such situation varies from an app to another. Sometimes a page refresh is the best thing to do, in other cases, instantiating a new Map
|
|
676
|
+
The best course of action in such situation varies from an app to another. Sometimes a page refresh is the best thing to do, in other cases, instantiating a new Map dynamically at application level is more appropriate because it hides a technical failure to the end user. The event `webglContextLost` is exposed so that the most appropriate scenario can be implemented at application level.
|
|
652
677
|
|
|
653
678
|
Here is how to respond to a WebGL context loss with a simple page refresh:
|
|
654
679
|
```ts
|
|
@@ -739,13 +764,13 @@ maptilersdk.helpers
|
|
|
739
764
|
|
|
740
765
|
**Example:** we have a *geoJSON* file that contains both *polygons* and *point* and we use it as the `data` property on the `helpers.addPoint(map, { options })`, this will only add the *points*.
|
|
741
766
|
|
|
742
|
-
In addition to easy styling, the
|
|
767
|
+
In addition to easy styling, the helpers' datasource can be:
|
|
743
768
|
- a URL to a geoJSON file or its string content
|
|
744
769
|
- a URL to a GPX or KML file (only for the polyline helper) or its string content
|
|
745
770
|
- a UUID of a MapTiler Cloud dataset
|
|
746
771
|
|
|
747
772
|
### Multiple Layers
|
|
748
|
-
The key design principle of these vector layer helpers is **it's easy to make what you want**, which is very different from **making MapLibre easier to use**.
|
|
773
|
+
The key design principle of these vector layer helpers is **it's easy to make what you want**, which is very different from **making MapLibre easier to use**.
|
|
749
774
|
|
|
750
775
|
> For example, to create a road with an outline, one must draw two layers: a wider base layer and a narrower top layer, fueled by the same polyline data. This requires ordering the layers properly and computing not the width of the outline, but rather the width of the polyline underneath so that it outgrows the top road layer of the desired number of pixels.
|
|
751
776
|
|
|
@@ -834,13 +859,12 @@ helpers.addPolyline(map, {
|
|
|
834
859
|
|
|
835
860
|
Endless possibilities, what about a glowing wire?
|
|
836
861
|
```ts
|
|
837
|
-
helpers.addPolyline(map, {
|
|
862
|
+
helpers.addPolyline(map, {
|
|
838
863
|
data: "74003ba7-215a-4b7e-8e26-5bbe3aa70b05",
|
|
839
864
|
lineColor: "#fff",
|
|
840
865
|
lineWidth: 1,
|
|
841
866
|
outline: true,
|
|
842
867
|
outlineColor: "#ca57ff",
|
|
843
|
-
outlineWidth: 2,
|
|
844
868
|
outlineWidth: 10,
|
|
845
869
|
outlineBlur: 10,
|
|
846
870
|
outlineOpacity: 0.5,
|
|
@@ -852,7 +876,7 @@ helpers.addPolyline(map, {
|
|
|
852
876
|
All the other options are documented on [our reference page](https://docs.maptiler.com/sdk-js/api/helpers/#polyline) and more examples are available [here](https://docs.maptiler.com/sdk-js/examples/?q=polyline+helper).
|
|
853
877
|
|
|
854
878
|
## Polygon Layer Helper
|
|
855
|
-
The polygon helper makes it easy to create vector layers that contain polygons, whether they are *multi*
|
|
879
|
+
The polygon helper makes it easy to create vector layers that contain polygons, whether they are *multi*polygons, *holed*polygons or just simple polygons. Whenever it's possible and it makes sense, we use the same terminology across the different helpers.
|
|
856
880
|
|
|
857
881
|
Here is a minimalist example, with a half-transparent polygon of Switzerland, from a local file:
|
|
858
882
|
|
package/tsconfig.json
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"baseUrl": "src",
|
|
4
4
|
"moduleResolution": "Node",
|
|
5
|
-
"target": "
|
|
5
|
+
"target": "es2021",
|
|
6
6
|
"useDefineForClassFields": true,
|
|
7
7
|
"module": "ESNext",
|
|
8
|
-
"lib": ["
|
|
8
|
+
"lib": ["es2021", "DOM", "DOM.Iterable"],
|
|
9
9
|
"skipLibCheck": true,
|
|
10
10
|
|
|
11
11
|
/* Bundler mode */
|