radar-sdk-js 4.2.0-beta.3 → 4.2.0-beta.5

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/README.md CHANGED
@@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ });
56
56
 
57
57
  Add the following script in your `html` file
58
58
  ```html
59
- <script src="https://js.radar.com/v4.2.0-beta.3/radar.min.js"></script>
59
+ <script src="https://js.radar.com/v4.2.0-beta.5/radar.min.js"></script>
60
60
  ```
61
61
 
62
62
  Then initialize the Radar SDK
@@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then
73
73
  ```html
74
74
  <html>
75
75
  <head>
76
- <link href="https://js.radar.com/v4.2.0-beta.3/radar.css" rel="stylesheet">
77
- <script src="https://js.radar.com/v4.2.0-beta.3/radar.min.js"></script>
76
+ <link href="https://js.radar.com/v4.2.0-beta.5/radar.css" rel="stylesheet">
77
+ <script src="https://js.radar.com/v4.2.0-beta.5/radar.min.js"></script>
78
78
  </head>
79
79
 
80
80
  <body>
@@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis
98
98
  ```html
99
99
  <html>
100
100
  <head>
101
- <link href="https://js.radar.com/v4.2.0-beta.3/radar.css" rel="stylesheet">
102
- <script src="https://js.radar.com/v4.2.0-beta.3/radar.min.js"></script>
101
+ <link href="https://js.radar.com/v4.2.0-beta.5/radar.css" rel="stylesheet">
102
+ <script src="https://js.radar.com/v4.2.0-beta.5/radar.min.js"></script>
103
103
  </head>
104
104
 
105
105
  <body>
@@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper
130
130
  ```html
131
131
  <html>
132
132
  <head>
133
- <link href="https://js.radar.com/v4.2.0-beta.3/radar.css" rel="stylesheet">
134
- <script src="https://js.radar.com/v4.2.0-beta.3/radar.min.js"></script>
133
+ <link href="https://js.radar.com/v4.2.0-beta.5/radar.css" rel="stylesheet">
134
+ <script src="https://js.radar.com/v4.2.0-beta.5/radar.min.js"></script>
135
135
  </head>
136
136
 
137
137
  <body>
package/dist/radar.js CHANGED
@@ -409,7 +409,7 @@ class Navigator {
409
409
  }
410
410
  }
411
411
 
412
- var SDK_VERSION = '4.2.0-beta.3';
412
+ var SDK_VERSION = '4.2.0-beta.5';
413
413
 
414
414
  class Http {
415
415
  static request({ method, path, data, host, versioned = true, headers = {}, }) {
@@ -465,6 +465,9 @@ class Http {
465
465
  Object.entries(allHeaders).forEach(([key, val]) => {
466
466
  xhr.setRequestHeader(key, val);
467
467
  });
468
+ if (allHeaders['Content-Type'] === 'image/png') {
469
+ xhr.responseType = 'blob';
470
+ }
468
471
  xhr.onload = () => {
469
472
  var _a;
470
473
  let response;
@@ -1406,73 +1409,6 @@ class VerifyAPI {
1406
1409
  }
1407
1410
  }
1408
1411
 
1409
- const defaultMarkerOptions = {
1410
- color: '#000257',
1411
- };
1412
- class RadarMarker extends maplibregl.Marker {
1413
- constructor(markerOptions) {
1414
- const maplibreOptions = Object.assign({}, defaultMarkerOptions);
1415
- if (markerOptions.color) {
1416
- maplibreOptions.color = markerOptions.color;
1417
- }
1418
- if (markerOptions.element) {
1419
- maplibreOptions.element = markerOptions.element;
1420
- }
1421
- if (markerOptions.scale) {
1422
- maplibreOptions.scale = markerOptions.scale;
1423
- }
1424
- if (markerOptions.image) {
1425
- const element = document.createElement('div');
1426
- element.style.backgroundImage = `url(${markerOptions.image.url})`;
1427
- element.style.width = markerOptions.image.width;
1428
- element.style.height = markerOptions.image.height;
1429
- maplibreOptions.element = element;
1430
- }
1431
- super(maplibreOptions);
1432
- if (markerOptions.image) {
1433
- this._image = markerOptions.image;
1434
- }
1435
- // set popup text or HTML
1436
- if (markerOptions.text) {
1437
- const popup = new maplibregl.Popup({ offset: 35 }).setText(markerOptions.text);
1438
- this.setPopup(popup);
1439
- }
1440
- else if (markerOptions.html) {
1441
- const popup = new maplibregl.Popup({ offset: 35 }).setHTML(markerOptions.html);
1442
- this.setPopup(popup);
1443
- }
1444
- }
1445
- addTo(map) {
1446
- if (map._customMarkerRawSvg && !this._image) {
1447
- this._element.innerHTML = map._customMarkerRawSvg;
1448
- }
1449
- map._markers.push(this);
1450
- return super.addTo(map);
1451
- }
1452
- remove() {
1453
- if (this._map) {
1454
- this._map._markers = this._map._markers.filter((marker) => marker !== this);
1455
- }
1456
- return super.remove();
1457
- }
1458
- getOptions() {
1459
- const markerOptions = {
1460
- // TODO: element: marker.getElement(),
1461
- image: this._image,
1462
- color: this._color,
1463
- scale: this._scale,
1464
- offset: this.getOffset(),
1465
- anchor: this._anchor,
1466
- draggable: this.isDraggable(),
1467
- clickTolerance: this._clickTolerance,
1468
- rotation: this.getRotation(),
1469
- rotationAlignment: this.getRotationAlignment(),
1470
- pitchAlignment: this.getPitchAlignment()
1471
- };
1472
- return markerOptions;
1473
- }
1474
- }
1475
-
1476
1412
  const RADAR_LOGO_URL = 'https://api.radar.io/maps/static/images/logo.svg';
1477
1413
  class RadarLogoControl {
1478
1414
  onAdd() {
@@ -1574,43 +1510,124 @@ class RadarMap extends maplibregl.Map {
1574
1510
  };
1575
1511
  this.on('resize', onResize);
1576
1512
  this.on('load', onResize);
1577
- const onStyleLoad = () => __awaiter(this, void 0, void 0, function* () {
1578
- var _a;
1579
- this._customMarkerRawSvg = undefined;
1580
- const style = this.getStyle();
1581
- const customMarkers = (_a = style.metadata) === null || _a === void 0 ? void 0 : _a['radar:customMarkers'];
1582
- if (Array.isArray(customMarkers) && customMarkers.length > 0) {
1583
- const customMarker = customMarkers[0]; // only support one custom marker for now
1584
- try {
1585
- const markerRawSvg = yield Http.request({
1586
- method: 'GET',
1587
- versioned: false,
1588
- path: `maps/markers/${customMarker.id}`,
1589
- headers: {
1590
- 'Content-Type': 'image/svg+xml',
1591
- },
1592
- });
1593
- this._customMarkerRawSvg = markerRawSvg.data;
1594
- }
1595
- catch (err) {
1596
- Logger.warn(`Error getting custom marker: ${customMarker.id} - using default marker.`);
1597
- }
1513
+ }
1514
+ getMarkers() {
1515
+ return this._markers;
1516
+ }
1517
+ }
1518
+
1519
+ const defaultMarkerOptions = {
1520
+ color: '#000257',
1521
+ };
1522
+ const fileExtensionToContentType = {
1523
+ svg: 'image/svg+xml',
1524
+ png: 'image/png',
1525
+ };
1526
+ const getFileExtension = (file) => {
1527
+ return file.split('.').pop() || '';
1528
+ };
1529
+ const createImageElement = (options) => {
1530
+ const element = document.createElement('img');
1531
+ element.src = options.url;
1532
+ if (options.width) {
1533
+ element.width = options.width;
1534
+ }
1535
+ if (options.height) {
1536
+ element.height = options.height;
1537
+ }
1538
+ element.style.maxWidth = '64px';
1539
+ element.style.maxHeight = '64px';
1540
+ return element;
1541
+ };
1542
+ class RadarMarker extends maplibregl.Marker {
1543
+ constructor(markerOptions) {
1544
+ const maplibreOptions = Object.assign({}, defaultMarkerOptions);
1545
+ if (markerOptions.color) {
1546
+ maplibreOptions.color = markerOptions.color;
1547
+ }
1548
+ if (markerOptions.element) {
1549
+ maplibreOptions.element = markerOptions.element;
1550
+ }
1551
+ if (markerOptions.scale) {
1552
+ maplibreOptions.scale = markerOptions.scale;
1553
+ }
1554
+ if (markerOptions.image) {
1555
+ maplibreOptions.element = createImageElement(markerOptions.image);
1556
+ }
1557
+ super(maplibreOptions);
1558
+ if (markerOptions.image) {
1559
+ this._image = markerOptions.image;
1560
+ }
1561
+ if (markerOptions.customMarkerId) {
1562
+ const ext = getFileExtension(markerOptions.customMarkerId);
1563
+ if (ext !== 'svg' && ext !== 'png') {
1564
+ Logger.error(`Invalid custom marker extension ${ext} - falling back to default marker`);
1598
1565
  }
1599
- // set markers if necessary
1600
- this._markers.forEach((marker) => {
1601
- if (this._customMarkerRawSvg && !marker._image) {
1602
- // set custom marker
1603
- marker._element.innerHTML = this._customMarkerRawSvg;
1604
- }
1605
- else {
1606
- const markerOptions = marker.getOptions();
1607
- const newMarker = new RadarMarker(markerOptions); // get default element
1608
- // set default element
1609
- marker._element.innerHTML = newMarker._element.innerHTML;
1610
- }
1611
- });
1612
- });
1613
- this.on('styledata', onStyleLoad);
1566
+ else {
1567
+ this._customMarkerId = markerOptions.customMarkerId;
1568
+ const originalElement = this._element.cloneNode(true);
1569
+ this._element.childNodes.forEach((child) => {
1570
+ child.remove();
1571
+ });
1572
+ const contentType = fileExtensionToContentType[ext];
1573
+ Http.request({
1574
+ method: 'GET',
1575
+ versioned: false,
1576
+ path: `maps/PlACEHOLDER/markers/${markerOptions.customMarkerId}`,
1577
+ headers: {
1578
+ 'Content-Type': contentType,
1579
+ },
1580
+ }).then((res) => {
1581
+ if (res.data instanceof Blob) {
1582
+ // png returns a Blob
1583
+ const customMarkerUrl = URL.createObjectURL(res.data);
1584
+ this._element.replaceChildren(createImageElement({ url: customMarkerUrl }));
1585
+ }
1586
+ else {
1587
+ // svg returns an xml string
1588
+ this._element.innerHTML = res.data;
1589
+ }
1590
+ }).catch((err) => {
1591
+ Logger.error(`Could not get custom marker: ${err.message} - falling back to default marker`);
1592
+ this._element.replaceChildren(...originalElement.childNodes);
1593
+ });
1594
+ }
1595
+ }
1596
+ // set popup text or HTML
1597
+ if (markerOptions.text) {
1598
+ const popup = new maplibregl.Popup({ offset: 35 }).setText(markerOptions.text);
1599
+ this.setPopup(popup);
1600
+ }
1601
+ else if (markerOptions.html) {
1602
+ const popup = new maplibregl.Popup({ offset: 35 }).setHTML(markerOptions.html);
1603
+ this.setPopup(popup);
1604
+ }
1605
+ }
1606
+ addTo(map) {
1607
+ map._markers.push(this);
1608
+ return super.addTo(map);
1609
+ }
1610
+ remove() {
1611
+ if (this._map) {
1612
+ this._map._markers = this._map._markers.filter((marker) => marker !== this);
1613
+ }
1614
+ return super.remove();
1615
+ }
1616
+ getOptions() {
1617
+ const markerOptions = {
1618
+ // TODO: element: marker.getElement(),
1619
+ image: this._image,
1620
+ color: this._color,
1621
+ scale: this._scale,
1622
+ offset: this.getOffset(),
1623
+ anchor: this._anchor,
1624
+ draggable: this.isDraggable(),
1625
+ clickTolerance: this._clickTolerance,
1626
+ rotation: this.getRotation(),
1627
+ rotationAlignment: this.getRotationAlignment(),
1628
+ pitchAlignment: this.getPitchAlignment()
1629
+ };
1630
+ return markerOptions;
1614
1631
  }
1615
1632
  }
1616
1633
 
package/dist/radar.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"radar.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/types.d.ts CHANGED
@@ -345,13 +345,14 @@ export interface RadarMapOptions extends maplibregl.MapOptions {
345
345
  }
346
346
  export interface RadarMarkerImage {
347
347
  url: string;
348
- width: string;
349
- height: string;
348
+ width?: number;
349
+ height?: number;
350
350
  }
351
351
  export interface RadarMarkerOptions extends maplibregl.MarkerOptions {
352
352
  text?: string;
353
353
  html?: string;
354
354
  image?: RadarMarkerImage;
355
+ customMarkerId?: string;
355
356
  }
356
357
  export interface RadarAutocompleteUIOptions {
357
358
  container: string | HTMLElement;
@@ -5,5 +5,6 @@ declare class RadarMap extends maplibregl.Map {
5
5
  _customMarkerRawSvg: string | undefined;
6
6
  _markers: RadarMarker[];
7
7
  constructor(mapOptions: RadarMapOptions);
8
+ getMarkers(): RadarMarker[];
8
9
  }
9
10
  export default RadarMap;
@@ -4,6 +4,7 @@ import type { RadarMarkerImage, RadarMarkerOptions } from '../types';
4
4
  declare class RadarMarker extends maplibregl.Marker {
5
5
  _map: RadarMap;
6
6
  _image?: RadarMarkerImage;
7
+ _customMarkerId?: string;
7
8
  constructor(markerOptions: RadarMarkerOptions);
8
9
  addTo(map: RadarMap): this;
9
10
  remove(): this;
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const _default: "4.2.0-beta.3";
1
+ declare const _default: "4.2.0-beta.5";
2
2
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "radar-sdk-js",
3
- "version": "4.2.0-beta.3",
3
+ "version": "4.2.0-beta.5",
4
4
  "description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
5
5
  "homepage": "https://radar.com",
6
6
  "type": "module",
package/src/http.ts CHANGED
@@ -105,6 +105,10 @@ class Http {
105
105
  xhr.setRequestHeader(key, val);
106
106
  });
107
107
 
108
+ if (allHeaders['Content-Type'] === 'image/png') {
109
+ xhr.responseType = 'blob';
110
+ }
111
+
108
112
  xhr.onload = () => {
109
113
  let response: any;
110
114
  try {
package/src/types.ts CHANGED
@@ -460,14 +460,15 @@ export interface RadarMapOptions extends maplibregl.MapOptions {
460
460
 
461
461
  export interface RadarMarkerImage {
462
462
  url: string;
463
- width: string;
464
- height: string;
463
+ width?: number;
464
+ height?: number;
465
465
  }
466
466
 
467
467
  export interface RadarMarkerOptions extends maplibregl.MarkerOptions {
468
468
  text?: string;
469
469
  html?: string;
470
470
  image?: RadarMarkerImage;
471
+ customMarkerId?: string;
471
472
  }
472
473
 
473
474
  export interface RadarAutocompleteUIOptions {
@@ -1,11 +1,10 @@
1
1
  import maplibregl from 'maplibre-gl';
2
2
 
3
3
  import RadarMarker from './RadarMarker';
4
+ import RadarLogoControl from './RadarLogoControl';
4
5
 
5
6
  import Config from '../config';
6
- import Http from '../http';
7
7
  import Logger from '../logger';
8
- import RadarLogoControl from './RadarLogoControl';
9
8
 
10
9
  import type { RadarOptions, RadarMapOptions } from '../types';
11
10
 
@@ -121,44 +120,10 @@ class RadarMap extends maplibregl.Map {
121
120
  };
122
121
  this.on('resize', onResize);
123
122
  this.on('load', onResize);
123
+ }
124
124
 
125
- const onStyleLoad = async () => {
126
- this._customMarkerRawSvg = undefined;
127
- const style = this.getStyle();
128
-
129
- const customMarkers = (style.metadata as any)?.['radar:customMarkers'];
130
- if (Array.isArray(customMarkers) && customMarkers.length > 0) {
131
- const customMarker = customMarkers[0]; // only support one custom marker for now
132
- try {
133
- const markerRawSvg = await Http.request({
134
- method: 'GET',
135
- versioned: false,
136
- path: `maps/markers/${customMarker.id}`,
137
- headers: {
138
- 'Content-Type': 'image/svg+xml',
139
- },
140
- });
141
- this._customMarkerRawSvg = markerRawSvg.data;
142
- } catch (err) {
143
- Logger.warn(`Error getting custom marker: ${customMarker.id} - using default marker.`);
144
- }
145
- }
146
-
147
- // set markers if necessary
148
- this._markers.forEach((marker) => {
149
- if (this._customMarkerRawSvg && !marker._image) {
150
- // set custom marker
151
- marker._element.innerHTML = this._customMarkerRawSvg;
152
- } else {
153
- const markerOptions = marker.getOptions();
154
- const newMarker = new RadarMarker(markerOptions); // get default element
155
-
156
- // set default element
157
- marker._element.innerHTML = newMarker._element.innerHTML;
158
- }
159
- });
160
- }
161
- this.on('styledata', onStyleLoad);
125
+ getMarkers(): RadarMarker[] {
126
+ return this._markers;
162
127
  }
163
128
  };
164
129
 
@@ -1,5 +1,8 @@
1
1
  import maplibregl from 'maplibre-gl';
2
2
 
3
+ import Http from '../http';
4
+ import Logger from '../logger';
5
+
3
6
  import type RadarMap from './RadarMap';
4
7
 
5
8
  import type { RadarMarkerImage, RadarMarkerOptions } from '../types';
@@ -8,9 +11,32 @@ const defaultMarkerOptions: maplibregl.MarkerOptions = {
8
11
  color: '#000257',
9
12
  };
10
13
 
14
+ const fileExtensionToContentType: Record<string, string> = {
15
+ svg: 'image/svg+xml',
16
+ png: 'image/png',
17
+ };
18
+
19
+ const getFileExtension = (file: string): string => {
20
+ return file.split('.').pop() || '';
21
+ }
22
+
23
+ const createImageElement = (options: RadarMarkerImage) => {
24
+ const element = document.createElement('img');
25
+ element.src = options.url;
26
+ if (options.width) {
27
+ element.width = options.width;
28
+ }
29
+ if (options.height) {
30
+ element.height = options.height;
31
+ }
32
+ element.style.maxWidth = '64px';
33
+ element.style.maxHeight = '64px';
34
+ return element;
35
+ }
11
36
  class RadarMarker extends maplibregl.Marker {
12
37
  _map!: RadarMap;
13
38
  _image?: RadarMarkerImage;
39
+ _customMarkerId?: string;
14
40
 
15
41
  constructor(markerOptions: RadarMarkerOptions) {
16
42
  const maplibreOptions: maplibregl.MarkerOptions = Object.assign({}, defaultMarkerOptions);
@@ -25,12 +51,7 @@ class RadarMarker extends maplibregl.Marker {
25
51
  maplibreOptions.scale = markerOptions.scale;
26
52
  }
27
53
  if (markerOptions.image) {
28
- const element = document.createElement('div');
29
- element.style.backgroundImage = `url(${markerOptions.image.url})`;
30
- element.style.width = markerOptions.image.width;
31
- element.style.height = markerOptions.image.height;
32
-
33
- maplibreOptions.element = element;
54
+ maplibreOptions.element = createImageElement(markerOptions.image);
34
55
  }
35
56
 
36
57
  super(maplibreOptions);
@@ -39,6 +60,42 @@ class RadarMarker extends maplibregl.Marker {
39
60
  this._image = markerOptions.image;
40
61
  }
41
62
 
63
+ if (markerOptions.customMarkerId) {
64
+ const ext = getFileExtension(markerOptions.customMarkerId);
65
+ if (ext !== 'svg' && ext !== 'png') {
66
+ Logger.error(`Invalid custom marker extension ${ext} - falling back to default marker`);
67
+ } else {
68
+ this._customMarkerId = markerOptions.customMarkerId;
69
+
70
+ const originalElement = this._element.cloneNode(true);
71
+ this._element.childNodes.forEach((child) => {
72
+ child.remove();
73
+ });
74
+ const contentType = fileExtensionToContentType[ext];
75
+
76
+ Http.request({
77
+ method: 'GET',
78
+ versioned: false,
79
+ path: `maps/PlACEHOLDER/markers/${markerOptions.customMarkerId}`,
80
+ headers: {
81
+ 'Content-Type': contentType,
82
+ },
83
+ }).then((res) => {
84
+ if (res.data instanceof Blob) {
85
+ // png returns a Blob
86
+ const customMarkerUrl = URL.createObjectURL(res.data);
87
+ this._element.replaceChildren(createImageElement({ url: customMarkerUrl }));
88
+ } else {
89
+ // svg returns an xml string
90
+ this._element.innerHTML = res.data;
91
+ }
92
+ }).catch((err) => {
93
+ Logger.error(`Could not get custom marker: ${err.message} - falling back to default marker`);
94
+ this._element.replaceChildren(...originalElement.childNodes);
95
+ });
96
+ }
97
+ }
98
+
42
99
  // set popup text or HTML
43
100
  if (markerOptions.text) {
44
101
  const popup = new maplibregl.Popup({ offset: 35 }).setText(markerOptions.text);
@@ -50,9 +107,6 @@ class RadarMarker extends maplibregl.Marker {
50
107
  }
51
108
 
52
109
  addTo(map: RadarMap) {
53
- if (map._customMarkerRawSvg && !this._image) {
54
- this._element.innerHTML = map._customMarkerRawSvg;
55
- }
56
110
  map._markers.push(this);
57
111
  return super.addTo(map);
58
112
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export default '4.2.0-beta.3';
1
+ export default '4.2.0-beta.5';