@abi-software/flatmap-viewer 2.7.0 → 2.7.1-a.2
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 +3 -2
- package/src/interactions.js +6 -0
- package/src/layers/cluster.js +4 -1
- package/src/layers/index.js +5 -1
- package/src/main.js +11 -2
- package/src/types.ts +26 -0
- package/src/utils.js +1 -0
- package/thirdParty/maplibre-gl-svg/CHANGELOG.md +13 -0
- package/thirdParty/maplibre-gl-svg/LICENSE +21 -0
- package/thirdParty/maplibre-gl-svg/LICENSE.md +21 -0
- package/thirdParty/maplibre-gl-svg/README.md +24 -0
- package/thirdParty/maplibre-gl-svg/assets/Add custom SVG template to template manager.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/All built-in SVG templates as HTML markers.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/All built-in SVG templates as symbols.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/Fill polygon with built-in SVG template.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/HTML Marker with Custom SVG Template.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/HTML Marker with built-in SVG template.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/Line layer with built-in SVG template.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/Load SVG from URL.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/SVG template options.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/Smiley_face_changed.svg +37 -0
- package/thirdParty/maplibre-gl-svg/assets/Symbol layer with built-in SVG template.jpg +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/arrow-up-thin.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/arrow-up.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/car.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/checker-rotated.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/checker.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/circles-spaced.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/circles.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/diagonal-lines-down.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/diagonal-lines-up.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/diagonal-stripes-down.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/diagonal-stripes-up.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/dots.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/flag-triangle.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/flag.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/grid-lines.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/hexagon-rounded-thick.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/hexagon-rounded.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/hexagon-thick.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/hexagon.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-arrow.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-ball-pin.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-circle.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-flat.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-square-cluster.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-square-rounded-cluster.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-square-rounded.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-square.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker-thick.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/marker.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/pin-round.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/pin.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/rotated-grid-lines.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/rotated-grid-stripes.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/rounded-square-thick.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/rounded-square.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/triangle-arrow-left.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/triangle-arrow-up.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/triangle-thick.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/triangle.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/x-fill.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/zig-zag-vertical.png +0 -0
- package/thirdParty/maplibre-gl-svg/assets/image-templates/zig-zag.png +0 -0
- package/thirdParty/maplibre-gl-svg/build/build.js +210 -0
- package/thirdParty/maplibre-gl-svg/dist/maplibre-gl-svg.js +339 -0
- package/thirdParty/maplibre-gl-svg/dist/maplibre-gl-svg.min.js +4 -0
- package/thirdParty/maplibre-gl-svg/docs/docs.md +375 -0
- package/thirdParty/maplibre-gl-svg/examples/Add custom SVG template to template manager.html +101 -0
- package/thirdParty/maplibre-gl-svg/examples/All built-in SVG templates as HTML markers.html +82 -0
- package/thirdParty/maplibre-gl-svg/examples/All built-in SVG templates as symbols.html +124 -0
- package/thirdParty/maplibre-gl-svg/examples/Fill polygon with built-in SVG template.html +94 -0
- package/thirdParty/maplibre-gl-svg/examples/HTML Marker with Custom SVG Template.html +86 -0
- package/thirdParty/maplibre-gl-svg/examples/HTML Marker with built-in SVG template.html +83 -0
- package/thirdParty/maplibre-gl-svg/examples/Line layer with built-in SVG template.html +129 -0
- package/thirdParty/maplibre-gl-svg/examples/Load SVG from URL.html +96 -0
- package/thirdParty/maplibre-gl-svg/examples/SVG template options.html +264 -0
- package/thirdParty/maplibre-gl-svg/examples/Symbol layer with built-in SVG template.html +93 -0
- package/thirdParty/maplibre-gl-svg/index.html +151 -0
- package/thirdParty/maplibre-gl-svg/package-lock.json +5882 -0
- package/thirdParty/maplibre-gl-svg/package.json +49 -0
- package/thirdParty/maplibre-gl-svg/src/SvgManager.ts +186 -0
- package/thirdParty/maplibre-gl-svg/src/SvgTemplateManager.ts +144 -0
- package/thirdParty/maplibre-gl-svg/src/index.ts +4 -0
- package/thirdParty/maplibre-gl-svg/tsconfig.json +31 -0
- package/thirdParty/maplibre-gl-svg/typings/index.d.ts +111 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
# How to the use the MapLibre GL SVG Plugin
|
|
2
|
+
|
|
3
|
+
Images can be used with HTML markers and various layers within MapLibre:
|
|
4
|
+
|
|
5
|
+
- Symbol layers can render points on the map with an image icon. Symbols can also be rendered along a lines path.
|
|
6
|
+
- Polygon/Fill layers can be rendered with a fill pattern image.
|
|
7
|
+
- HTML markers can render points using images and other HTML elements.
|
|
8
|
+
|
|
9
|
+
Be sure to check out the [examples page](https://rbrundritt.github.io/maplibre-gl-svg/index.html).
|
|
10
|
+
|
|
11
|
+
- **Content**
|
|
12
|
+
- [How to the use the MapLibre GL SVG Plugin](#how-to-the-use-the-maplibre-gl-svg-plugin)
|
|
13
|
+
- [Getting started](#getting-started)
|
|
14
|
+
- [Use the SvgTemplateManager](#use-the-svgtemplatemanager)
|
|
15
|
+
- [Use an SVG template with an HTML marker](#use-an-svg-template-with-an-html-marker)
|
|
16
|
+
- [Use an SVG along a lines path](#use-an-svg-along-a-lines-path)
|
|
17
|
+
- [Use an SVG with a fill layer](#use-an-svg-with-a-fill-layer)
|
|
18
|
+
- [Create a custom SVG template](#create-a-custom-svg-template)
|
|
19
|
+
- [List of image templates](#list-of-image-templates)
|
|
20
|
+
|
|
21
|
+
## Getting started
|
|
22
|
+
|
|
23
|
+
Download the project and copy the `maplibre-gl-svg` JavaScript file from the `dist` folder into your project.
|
|
24
|
+
|
|
25
|
+
> NPM package may be made available in the future once the initial version is locked in.
|
|
26
|
+
|
|
27
|
+
This plugin has two main classes;
|
|
28
|
+
|
|
29
|
+
- `SvgManager` - A class that takes in a map instance and that makes it easy to load SVGs into the map sprite and manages the life cycle of SVG images within the map.
|
|
30
|
+
- `SvgTemplateManager` - A static class the manages reusable SVG templates.
|
|
31
|
+
|
|
32
|
+
The `SvgManager` class provides the following functions.
|
|
33
|
+
|
|
34
|
+
| Name | Return Type | Description |
|
|
35
|
+
|------|-------------|-------------|
|
|
36
|
+
| `add(id: string, svg: string, maxWidth?: number, maxHeight?: number)` | `Promise<void>` | Adds an SVG image to the maps image sprite. The SVG can be either a URL to an SVG file, an inline SVG string, or SVG data URI. A max width or height will be used to scale down an SVG if needed. By default these max values are set to 100 pixels. |
|
|
37
|
+
| `clear()` | | Removes all SVG images loaded into the maps sprite. |
|
|
38
|
+
| `getImageIds()`| `string[]` | Gets a list of all the image ids that have been added to the maps image sprite. |
|
|
39
|
+
| `hasImage(id: string)` | `boolean` | Checks to see if an image is already loaded into the maps image sprite. |
|
|
40
|
+
| `createFromTemplate(id: string, templateName: string, color?: string, secondaryColor?: string, scale?: number, text?: string)` | `Promise<void>` | Creates and adds an image to the maps image sprite from an SVG template. Provide the name of the built-in or pre-loaded template to use, and a color to apply. |
|
|
41
|
+
| `remove(id: string)` | | Removes an SVG image from the maps image sprite. |
|
|
42
|
+
| `reload()` | | It's possible that the map's image sprite may be cleared by the user outside of this manager. Calling this method will reload all images in this manager. |
|
|
43
|
+
|
|
44
|
+
SVG templates can be added to the map image sprite resources by using the `createFromTemplate` function of an `SvgManager` instance. This function allows up to five parameters to be passed in;
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
createFromTemplate(id: string, templateName: string, color: string = '#1A73AA', secondaryColor: string = 'white', scale: number = 1, text = ''): Promise<void>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The `id` is a unique identifier you create. The `id` is assigned to the image when it's added to the maps image sprite. Use this identifier in the layers to specifying which image resource to render. The `templateName` specifies which image template to use. The `color` option sets the primary color of the image and the `secondaryColor` options sets the secondary color of the image. The `scale` option scales the image template before applying it to the image sprite. When the image is applied to the image sprite, it's converted into a PNG. To ensure crisp rendering, it's better to scale up the image template before adding it to the sprite, than to scale it up in a layer.
|
|
51
|
+
|
|
52
|
+
This function asynchronously loads the image into the image sprite. Thus, it returns a `Promise` that you can wait for this function to complete.
|
|
53
|
+
|
|
54
|
+
The following code shows how to create an image from one of the built-in templates, and use it with a symbol layer.
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
//Create a new instance of the SVG manager.
|
|
58
|
+
var svgManager = new maplibregl.SvgManager(map);
|
|
59
|
+
|
|
60
|
+
//Load an SVG templated icon into the map.
|
|
61
|
+
svgManager.createFromTemplate('myTemplatedImage', 'marker-flat', 'teal', '#fff').then(() => {
|
|
62
|
+
|
|
63
|
+
//Add a layer to display a filled polygon.
|
|
64
|
+
map.addLayer({
|
|
65
|
+
'id': 'symbol-layer',
|
|
66
|
+
'type': 'symbol',
|
|
67
|
+
'source': {
|
|
68
|
+
'type': 'geojson',
|
|
69
|
+
'data': {
|
|
70
|
+
'type': 'FeatureCollection',
|
|
71
|
+
'features': [
|
|
72
|
+
{
|
|
73
|
+
'type': 'Feature',
|
|
74
|
+
'properties': {
|
|
75
|
+
},
|
|
76
|
+
'geometry': {
|
|
77
|
+
'type': 'Point',
|
|
78
|
+
'coordinates': [0,0]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
]
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
'layout': {
|
|
85
|
+
//Reference the templated icon.
|
|
86
|
+
'icon-image': 'myTemplatedImage'
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
In order to ensure good performance with layers, load the images into the map image sprite before rendering.
|
|
93
|
+
|
|
94
|
+
> **NOTE**
|
|
95
|
+
> When loaded into the maps image sprite SVG's are converted into a data URI, and loaded as a static image into the maps image sprite. This means animations and CSS classes are not supported. All CSS styles should be embedded within the SVG. SVG's used with HTML markers will support CSS classes and animations as usual.
|
|
96
|
+
|
|
97
|
+
## Use the SvgTemplateManager
|
|
98
|
+
|
|
99
|
+
If your application uses the same image with different styles, possible with multiple map instances, you can create reusable SVG templates which allow you to programmatically generate styled versions of your images. The static `maplibregl.SvgTemplateManager` class provides the following functions.
|
|
100
|
+
|
|
101
|
+
| Name | Return Type | Description |
|
|
102
|
+
|------|-------------|-------------|
|
|
103
|
+
| `addTemplate(templateName: string, template: string, override: boolean)` | | Adds an image template to the SvgTemplateManager. |
|
|
104
|
+
| `getElement(templateName: string, text?: string, color?: string, secondaryColor?: string, scale?: number)` | `HTMLElement` | Inflates a template and converts it into an HTMLElement. |
|
|
105
|
+
| `getTemplate(templateName: string, scale?: number)`| string | Retrieves an SVG template by name. |
|
|
106
|
+
| `getAllTemplateNames()` | `string[]` | Retrieves an SVG template by name. |
|
|
107
|
+
|
|
108
|
+
## Use an SVG template with an HTML marker
|
|
109
|
+
|
|
110
|
+
An SVG template can be retrieved using the `maplibregl.SvgTemplateManager.getElement` function to retrieve an HTML element rendered version of the SVG template. This HTML element can then be passed into the markers `element` option.
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
// Create marker element from SVG template.
|
|
114
|
+
const el = maplibregl.SvgTemplateManager.getElement('marker-arrow', 'AB', 'red', 'pink');
|
|
115
|
+
|
|
116
|
+
// Add marker to map
|
|
117
|
+
marker = new maplibregl.Marker({
|
|
118
|
+
element: el,
|
|
119
|
+
anchor: 'bottom'
|
|
120
|
+
}).setLngLat(center).addTo(map);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
> **TIP**
|
|
124
|
+
> SVG templates can be used outside of the map too. The `getTemplate` funciton returns an SVG string that has placeholders; `{color}`, `{secondaryColor}`, `{scale}`, `{text}`. Replace these placeholder values to create a valid SVG string. You can then either add the SVG string directly to the HTML DOM, retrieve an HTML element version of the rendered template, or convert it into a data URI and insert it into an image tag or canvas. For example:
|
|
125
|
+
>
|
|
126
|
+
> ```JavaScript
|
|
127
|
+
> //Get an HTML element version of the template.
|
|
128
|
+
> var elm = maplibregl.SvgTemplateManager.getElement('marker', '', 'red', 'white', 1);
|
|
129
|
+
>
|
|
130
|
+
> //Retrieve an SVG template string and replace the placeholder values.
|
|
131
|
+
> var svg = maplibregl.SvgTemplateManager.getTemplate('marker').replace(/{color}/, 'red').replace(/{secondaryColor}/, 'white').replace(/{text}/, '').replace(/{scale}/, 1);
|
|
132
|
+
>
|
|
133
|
+
> //Convert to data URI for use in image tags.
|
|
134
|
+
> var dataUri = 'data:image/svg+xml;base64,' + btoa(svg);
|
|
135
|
+
> ```
|
|
136
|
+
|
|
137
|
+
## Use an SVG along a lines path
|
|
138
|
+
|
|
139
|
+
Once an SVG is loaded into the map image sprite, it can be rendered along the path of a line by adding a `LineString` to a data source and using a symbol layer with a `symbol-placement` option set to `line` and by referencing the ID of the image resource in the `icon-image` option.
|
|
140
|
+
|
|
141
|
+
```javascript
|
|
142
|
+
//Create a new instance of the SVG manager.
|
|
143
|
+
var svgManager = new maplibregl.SvgManager(map);
|
|
144
|
+
|
|
145
|
+
//Load an SVG templated icon into the map.
|
|
146
|
+
svgManager.createFromTemplate('myTemplatedImage', 'car', 'DodgerBlue', '#fff').then(() => {
|
|
147
|
+
|
|
148
|
+
//Add a source for thelines.
|
|
149
|
+
map.addSource('lines', {
|
|
150
|
+
'type': 'geojson',
|
|
151
|
+
'data': {
|
|
152
|
+
'type': 'FeatureCollection',
|
|
153
|
+
'features': [
|
|
154
|
+
{
|
|
155
|
+
'type': 'Feature',
|
|
156
|
+
'properties': {
|
|
157
|
+
},
|
|
158
|
+
'geometry': {
|
|
159
|
+
'type': 'LineString',
|
|
160
|
+
'coordinates': [
|
|
161
|
+
[-122.18822, 47.63208],
|
|
162
|
+
[-122.18204, 47.63196],
|
|
163
|
+
[-122.17243, 47.62976],
|
|
164
|
+
[-122.16419, 47.63023],
|
|
165
|
+
[-122.15852, 47.62942],
|
|
166
|
+
[-122.15183, 47.62988],
|
|
167
|
+
[-122.14256, 47.63451],
|
|
168
|
+
[-122.13483, 47.64041],
|
|
169
|
+
[-122.13466, 47.64422],
|
|
170
|
+
[-122.13844, 47.65440],
|
|
171
|
+
[-122.13277, 47.66515],
|
|
172
|
+
[-122.12779, 47.66712],
|
|
173
|
+
[-122.11595, 47.66712],
|
|
174
|
+
[-122.11063, 47.66735],
|
|
175
|
+
[-122.10668, 47.67035],
|
|
176
|
+
[-122.10565, 47.67498]
|
|
177
|
+
]
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
]
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
//Add a layer to display the lines.
|
|
185
|
+
map.addLayer({
|
|
186
|
+
'id': 'lines',
|
|
187
|
+
'type': 'line',
|
|
188
|
+
'source': 'lines',
|
|
189
|
+
'paint': {
|
|
190
|
+
'line-width': 5,
|
|
191
|
+
'line-color': 'Blue'
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
//Add a layer for symbols along the line.
|
|
196
|
+
map.addLayer({
|
|
197
|
+
'id': 'line-symbols',
|
|
198
|
+
'type': 'symbol',
|
|
199
|
+
'source': 'lines',
|
|
200
|
+
'layout': {
|
|
201
|
+
//Reference the templated icon.
|
|
202
|
+
'icon-image': 'myTemplatedImage',
|
|
203
|
+
'symbol-placement': 'line',
|
|
204
|
+
'symbol-spacing': 100,
|
|
205
|
+
'icon-rotate': 90
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
> **TIP**
|
|
212
|
+
> If the SVG template points up, set the `rotation` icon option of the symbol layer to 90 if you want it to point in the same direction as the line.
|
|
213
|
+
|
|
214
|
+
## Use an SVG with a fill layer
|
|
215
|
+
|
|
216
|
+
Once an SVG template is loaded into the map image sprite, it can be rendered as a fill pattern in a fill layer by referencing the image resource ID in the `fill-pattern` option of the layer.
|
|
217
|
+
|
|
218
|
+
The [Fill polygon with built-in icon template] sample demonstrates how to render a polygon layer using the `dot` image template with a red primary color and a transparent secondary color, as shown in the following screenshot. For the source code for this sample, see [Fill polygon with built-in icon template sample code].
|
|
219
|
+
|
|
220
|
+
```javascript
|
|
221
|
+
//Create a new instance of the SVG manager.
|
|
222
|
+
var svgManager = new maplibregl.SvgManager(map);
|
|
223
|
+
|
|
224
|
+
//Load an SVG templated icon into the map.
|
|
225
|
+
svgManager.createFromTemplate('myFillPattern', 'dots', 'red', 'transparent').then(() => {
|
|
226
|
+
|
|
227
|
+
//Add a layer to display a filled polygon.
|
|
228
|
+
map.addLayer({
|
|
229
|
+
'id': 'fill-layer',
|
|
230
|
+
'type': 'fill',
|
|
231
|
+
'source': {
|
|
232
|
+
'type': 'geojson',
|
|
233
|
+
'data': {
|
|
234
|
+
'type': 'FeatureCollection',
|
|
235
|
+
'features': [
|
|
236
|
+
{
|
|
237
|
+
'type': 'Feature',
|
|
238
|
+
'properties': {
|
|
239
|
+
},
|
|
240
|
+
'geometry': {
|
|
241
|
+
'type': 'Polygon',
|
|
242
|
+
'coordinates': [[[-50, -20], [0, 40], [50, -20], [-50, -20]]]
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
]
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
'paint': {
|
|
249
|
+
//Apply the fill pattern.
|
|
250
|
+
'fill-pattern': 'myFillPattern',
|
|
251
|
+
'fill-opacity': 1
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
> **TIP**
|
|
258
|
+
> Setting an alpha value on the secondary color of fill patterns makes it easier to see the underlying map while still providing the primary pattern.
|
|
259
|
+
|
|
260
|
+
## Create a custom SVG template
|
|
261
|
+
|
|
262
|
+
SVG image templates are inline SVG strings that support the following placeholder values:
|
|
263
|
+
|
|
264
|
+
| Placeholder | Description |
|
|
265
|
+
|-------------|--------------------|
|
|
266
|
+
| `{color}` | The primary color. |
|
|
267
|
+
| `{secondaryColor}` | The secondary color. |
|
|
268
|
+
| `{scale}` | The SVG image is converted to an png image when added to the map image sprite. This placeholder can be used to scale a template before it's converted to ensure it renders clearly. |
|
|
269
|
+
| `{text}` | The location to render text when used with an HTML Marker. |
|
|
270
|
+
|
|
271
|
+
Below is an example of how to create a custom SVG template string, add it to the `SvgTemplateManager` and use it as a polygon fill pattern .
|
|
272
|
+
|
|
273
|
+
```javascript
|
|
274
|
+
//A custom icon template created using an inline SVG string with placeholders for {scale}, {color}, and {secondaryColor}.
|
|
275
|
+
var customTemplate = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80" width="calc(80px * {scale})" height="calc(80px * {scale})"><rect x="0" y="0" width="80" height="80" fill="{secondaryColor}"/><path fill="{color}" d="M14 16H9v-2h5V9.87a4 4 0 1 1 2 0V14h5v2h-5v15.95A10 10 0 0 0 23.66 27l-3.46-2 8.2-2.2-2.9 5a12 12 0 0 1-21 0l-2.89-5 8.2 2.2-3.47 2A10 10 0 0 0 14 31.95V16zm40 40h-5v-2h5v-4.13a4 4 0 1 1 2 0V54h5v2h-5v15.95A10 10 0 0 0 63.66 67l-3.47-2 8.2-2.2-2.88 5a12 12 0 0 1-21.02 0l-2.88-5 8.2 2.2-3.47 2A10 10 0 0 0 54 71.95V56zm-39 6a2 2 0 1 1 0-4 2 2 0 0 1 0 4zm40-40a2 2 0 1 1 0-4 2 2 0 0 1 0 4zM15 8a2 2 0 1 0 0-4 2 2 0 0 0 0 4zm40 40a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"></path></svg>';
|
|
276
|
+
|
|
277
|
+
//Add the custom template to the SVG template manager.
|
|
278
|
+
maplibregl.SvgTemplateManager.addTemplate('anchor-fill', customTemplate);
|
|
279
|
+
|
|
280
|
+
//Create a new instance of the SVG manager.
|
|
281
|
+
svgManager = new maplibregl.SvgManager(map);
|
|
282
|
+
|
|
283
|
+
//Load an SVG templated icon into the map.
|
|
284
|
+
svgManager.createFromTemplate('myFillPattern', 'anchor-fill', 'navy', 'rgba(0,150,150,0.5)').then(() => {
|
|
285
|
+
|
|
286
|
+
//Add a layer to display a filled polygon.
|
|
287
|
+
map.addLayer({
|
|
288
|
+
'id': 'fill-layer',
|
|
289
|
+
'type': 'fill',
|
|
290
|
+
'source': {
|
|
291
|
+
'type': 'geojson',
|
|
292
|
+
'data': {
|
|
293
|
+
'type': 'FeatureCollection',
|
|
294
|
+
'features': [
|
|
295
|
+
{
|
|
296
|
+
'type': 'Feature',
|
|
297
|
+
'properties': {
|
|
298
|
+
},
|
|
299
|
+
'geometry': {
|
|
300
|
+
'type': 'Polygon',
|
|
301
|
+
'coordinates': [[[-50, -20], [0, 40], [50, -20], [-50, -20]]]
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
]
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
'paint': {
|
|
308
|
+
//Apply the fill pattern.
|
|
309
|
+
'fill-pattern': 'myFillPattern',
|
|
310
|
+
'fill-opacity': 1
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
> **TIP**
|
|
317
|
+
> Custom SVG template strings should have a `viewbox`, `width`, and `height` specified to represent the expected default size. Use CSS `calc` in the `width` and `height` properties to support scaling the template.
|
|
318
|
+
|
|
319
|
+
## List of image templates
|
|
320
|
+
|
|
321
|
+
In total there are 42 image templates provided: 27 symbol icons and 15 polygon fill patterns. This table lists all image templates currently available within the `SvgTemplateManager`. By default, the primary color is blue and the secondary color is white. To make the secondary color easier to see on a white background, the following images have the secondary color set to black for documentation purposes.
|
|
322
|
+
|
|
323
|
+
**Symbol icon templates**
|
|
324
|
+
|
|
325
|
+
| Template name | Template |
|
|
326
|
+
|---------------|----------|
|
|
327
|
+
| `marker` |  |
|
|
328
|
+
| `marker-thick` |  |
|
|
329
|
+
| `marker-circle` |  |
|
|
330
|
+
| `pin` |  |
|
|
331
|
+
| `pin-round` |  |
|
|
332
|
+
| `marker-flat` |  |
|
|
333
|
+
| `marker-arrow` |  |
|
|
334
|
+
| `marker-ball-pin` |  |
|
|
335
|
+
| `marker-square` |  |
|
|
336
|
+
| `marker-square-cluster` |  |
|
|
337
|
+
| `marker-square-rounded` |  |
|
|
338
|
+
| `marker-square-rounded-cluster` |  |
|
|
339
|
+
| `flag` |  |
|
|
340
|
+
| `flag-triangle` |  |
|
|
341
|
+
| `rounded-square` |  |
|
|
342
|
+
| `rounded-square-thick` |  |
|
|
343
|
+
| `triangle` |  |
|
|
344
|
+
| `triangle-thick` |  |
|
|
345
|
+
| `hexagon` |  |
|
|
346
|
+
| `hexagon-thick` |  |
|
|
347
|
+
| `hexagon-rounded` |  |
|
|
348
|
+
| `hexagon-rounded-thick` |  |
|
|
349
|
+
| `triangle-arrow-up` |  |
|
|
350
|
+
| `triangle-arrow-left` |  |
|
|
351
|
+
| `arrow-up` |  |
|
|
352
|
+
| `arrow-up-thin` |  |
|
|
353
|
+
| `car` |  |
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
**Polygon fill pattern templates**
|
|
357
|
+
|
|
358
|
+
| Template name | Template |
|
|
359
|
+
|---------------|----------|
|
|
360
|
+
| `checker` |  |
|
|
361
|
+
| `checker-rotated` |  |
|
|
362
|
+
| `zig-zag` |  |
|
|
363
|
+
| `zig-zag-vertical` |  |
|
|
364
|
+
| `circles-spaced` |  |
|
|
365
|
+
| `circles` |  |
|
|
366
|
+
| `diagonal-lines-up` |  |
|
|
367
|
+
| `diagonal-lines-down` |  |
|
|
368
|
+
| `diagonal-stripes-up` |  |
|
|
369
|
+
| `diagonal-stripes-down` |  |
|
|
370
|
+
| `grid-lines` |  |
|
|
371
|
+
| `rotated-grid-lines` |  |
|
|
372
|
+
| `rotated-grid-stripes` |  |
|
|
373
|
+
| `x-fill` |  |
|
|
374
|
+
| `dots` |  |
|
|
375
|
+
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<title>Add custom SVG template to template manager</title>
|
|
5
|
+
<meta charset='utf-8'>
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
7
|
+
<link rel='stylesheet' href='https://unpkg.com/maplibre-gl@3.6.2/dist/maplibre-gl.css' />
|
|
8
|
+
<script src='https://unpkg.com/maplibre-gl@3.6.2/dist/maplibre-gl.js'></script>
|
|
9
|
+
|
|
10
|
+
<script src="../dist/maplibre-gl-svg.js"></script>
|
|
11
|
+
|
|
12
|
+
<style>
|
|
13
|
+
body { margin: 0; padding: 0; }
|
|
14
|
+
html, body { height: 100%; }
|
|
15
|
+
#map { position: relative; width:100%; height:100%; }
|
|
16
|
+
fieldset { position: absolute; top: 10px; left: 10px; width: 300px;z-index: 1; background-color: white;}
|
|
17
|
+
legend {background-color: white;border-radius: 5px;border:1px solid #000;}
|
|
18
|
+
</style>
|
|
19
|
+
</head>
|
|
20
|
+
<body>
|
|
21
|
+
<div id="map"></div>
|
|
22
|
+
|
|
23
|
+
<fieldset>
|
|
24
|
+
<legend>Add custom SVG template to template manager</legend>
|
|
25
|
+
This sample shows how to add more image templates to the SVG template manager.
|
|
26
|
+
</fieldset>
|
|
27
|
+
|
|
28
|
+
<script>
|
|
29
|
+
var marker, svgManager;
|
|
30
|
+
|
|
31
|
+
//A custom icon template created using an inline SVG string with placeholders for {scale}, {color}, and {secondaryColor}.
|
|
32
|
+
var customTemplate = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80" width="calc(80px * {scale})" height="calc(80px * {scale})"><rect x="0" y="0" width="80" height="80" fill="{secondaryColor}"/><path fill="{color}" d="M14 16H9v-2h5V9.87a4 4 0 1 1 2 0V14h5v2h-5v15.95A10 10 0 0 0 23.66 27l-3.46-2 8.2-2.2-2.9 5a12 12 0 0 1-21 0l-2.89-5 8.2 2.2-3.47 2A10 10 0 0 0 14 31.95V16zm40 40h-5v-2h5v-4.13a4 4 0 1 1 2 0V54h5v2h-5v15.95A10 10 0 0 0 63.66 67l-3.47-2 8.2-2.2-2.88 5a12 12 0 0 1-21.02 0l-2.88-5 8.2 2.2-3.47 2A10 10 0 0 0 54 71.95V56zm-39 6a2 2 0 1 1 0-4 2 2 0 0 1 0 4zm40-40a2 2 0 1 1 0-4 2 2 0 0 1 0 4zM15 8a2 2 0 1 0 0-4 2 2 0 0 0 0 4zm40 40a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"></path></svg>';
|
|
33
|
+
|
|
34
|
+
const map = new maplibregl.Map({
|
|
35
|
+
container: 'map',
|
|
36
|
+
style: {
|
|
37
|
+
'version': 8,
|
|
38
|
+
'sources': {
|
|
39
|
+
'osm-tiles': {
|
|
40
|
+
'type': 'raster',
|
|
41
|
+
'tiles': [ 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' ],
|
|
42
|
+
'tileSize': 256,
|
|
43
|
+
'attribution': '© <a href="https://www.openstreetmap.org/about" target="_blank">OpenStreetMap</a>'
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
'layers': [
|
|
47
|
+
{
|
|
48
|
+
'id': 'osm-tiles',
|
|
49
|
+
'type': 'raster',
|
|
50
|
+
'source': 'osm-tiles',
|
|
51
|
+
'minzoom': 0,
|
|
52
|
+
'maxzoom': 19
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
zoom: 2
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
map.on('load', () => {
|
|
60
|
+
|
|
61
|
+
//Add the custom template to the SVG template manager.
|
|
62
|
+
maplibregl.SvgTemplateManager.addTemplate('anchor-fill', customTemplate);
|
|
63
|
+
|
|
64
|
+
//Create a new instance of the SVG manager.
|
|
65
|
+
svgManager = new maplibregl.SvgManager(map);
|
|
66
|
+
|
|
67
|
+
//Load an SVG templated icon into the map.
|
|
68
|
+
svgManager.createFromTemplate('myFillPattern', 'anchor-fill', 'navy', 'rgba(0,150,150,0.5)').then(() => {
|
|
69
|
+
|
|
70
|
+
//Add a layer to display a filled polygon.
|
|
71
|
+
map.addLayer({
|
|
72
|
+
'id': 'fill-layer',
|
|
73
|
+
'type': 'fill',
|
|
74
|
+
'source': {
|
|
75
|
+
'type': 'geojson',
|
|
76
|
+
'data': {
|
|
77
|
+
'type': 'FeatureCollection',
|
|
78
|
+
'features': [
|
|
79
|
+
{
|
|
80
|
+
'type': 'Feature',
|
|
81
|
+
'properties': {
|
|
82
|
+
},
|
|
83
|
+
'geometry': {
|
|
84
|
+
'type': 'Polygon',
|
|
85
|
+
'coordinates': [[[-50, -20], [0, 40], [50, -20], [-50, -20]]]
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
]
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
'paint': {
|
|
92
|
+
//Apply the fill pattern.
|
|
93
|
+
'fill-pattern': 'myFillPattern',
|
|
94
|
+
'fill-opacity': 1
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
</script>
|
|
100
|
+
</body>
|
|
101
|
+
</html>
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<title>All built-in SVG templates as HTML markers</title>
|
|
5
|
+
<meta charset='utf-8'>
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
7
|
+
<link rel='stylesheet' href='https://unpkg.com/maplibre-gl@3.6.2/dist/maplibre-gl.css' />
|
|
8
|
+
<script src='https://unpkg.com/maplibre-gl@3.6.2/dist/maplibre-gl.js'></script>
|
|
9
|
+
|
|
10
|
+
<script src="../dist/maplibre-gl-svg.js"></script>
|
|
11
|
+
|
|
12
|
+
<style>
|
|
13
|
+
body { margin: 0; padding: 0; }
|
|
14
|
+
html, body { height: 100%; }
|
|
15
|
+
#map { position: relative; width:100%; height:100%; }
|
|
16
|
+
fieldset { position: absolute; top: 10px; left: 10px; width: 300px;z-index: 1; background-color: white;}
|
|
17
|
+
legend {background-color: white;border-radius: 5px;border:1px solid #000;}
|
|
18
|
+
</style>
|
|
19
|
+
</head>
|
|
20
|
+
<body>
|
|
21
|
+
<div id="map"></div>
|
|
22
|
+
|
|
23
|
+
<fieldset>
|
|
24
|
+
<legend>All built-in SVG templates as HTML markers</legend>
|
|
25
|
+
This sample shows all the built-in SVG templates rendered as HTML markers. "00" is added as the text value for each icon so that you can see where the text will render on the marker.
|
|
26
|
+
</fieldset>
|
|
27
|
+
|
|
28
|
+
<script>
|
|
29
|
+
const center = [20, -10];
|
|
30
|
+
var marker;
|
|
31
|
+
|
|
32
|
+
const map = new maplibregl.Map({
|
|
33
|
+
container: 'map',
|
|
34
|
+
center: center,
|
|
35
|
+
zoom: 4,
|
|
36
|
+
style: {
|
|
37
|
+
'version': 8,
|
|
38
|
+
'sources': {
|
|
39
|
+
},
|
|
40
|
+
'layers': [
|
|
41
|
+
{
|
|
42
|
+
"id": "background",
|
|
43
|
+
"type": "background",
|
|
44
|
+
"paint": {
|
|
45
|
+
"background-color": "gray"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
map.on('load', () => {
|
|
53
|
+
//Get all image templates available in the map control.
|
|
54
|
+
var keys = maplibregl.SvgTemplateManager.getAllTemplateNames();
|
|
55
|
+
|
|
56
|
+
//Create a grid of the HTML markers, each using a different SVG template.
|
|
57
|
+
var cols = 6;
|
|
58
|
+
var rows = Math.ceil(keys.length / cols);
|
|
59
|
+
|
|
60
|
+
for (var row = 0; row < rows; row++) {
|
|
61
|
+
for (var col = 0; col < cols; col++) {
|
|
62
|
+
var idx = cols * row + col;
|
|
63
|
+
if (idx < keys.length) {
|
|
64
|
+
createIcon(keys[idx], row, col);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
function createIcon(id, row, col) {
|
|
71
|
+
// Create marker element from SVG template.
|
|
72
|
+
//Add '00' as placeholder for where text would be rendered on the icon.
|
|
73
|
+
const el = maplibregl.SvgTemplateManager.getElement(id, '00');
|
|
74
|
+
|
|
75
|
+
new maplibregl.Marker({
|
|
76
|
+
element: el,
|
|
77
|
+
anchor: 'bottom'
|
|
78
|
+
}).setLngLat( [8 * col, -3.5 * row]).addTo(map);
|
|
79
|
+
}
|
|
80
|
+
</script>
|
|
81
|
+
</body>
|
|
82
|
+
</html>
|