diff --git a/demo/views/add-a-marker.hbs b/demo/views/add-a-marker.hbs index 3da2f11..fbd762f 100644 --- a/demo/views/add-a-marker.hbs +++ b/demo/views/add-a-marker.hbs @@ -43,7 +43,11 @@ map.on('load', () => { const { lat, lng } = map.getCenter(); - marker = Radar.ui.marker({ text: `${lat}, ${lng}` }) + marker = Radar.ui.marker({ + popup: { + text: `${lat}, ${lng}`, + }, + }) .setLngLat([lng, lat]) .addTo(map); @@ -59,8 +63,10 @@ } const { lat, lng } = map.getCenter(); marker = Radar.ui.marker({ - image: { url }, - text: `${lat}, ${lng}`, + url, + popup: { + text: `${lat}, ${lng}`, + }, }) .setLngLat([lng, lat]) .addTo(map); diff --git a/demo/views/start-a-trip.hbs b/demo/views/start-a-trip.hbs index 8a8bd13..fd2a5b0 100644 --- a/demo/views/start-a-trip.hbs +++ b/demo/views/start-a-trip.hbs @@ -111,7 +111,11 @@ <p>${location.latitude},${location.longitude}</p> `.trim(); - const userMarker = Radar.ui.marker({ html: userContent }) + const userMarker = Radar.ui.marker({ + popup: { + html: userContent, + }, + }) .setLngLat([location.longitude, location.latitude]) .addTo(map); @@ -122,7 +126,12 @@ <p>${user.trip.destinationGeofenceExternalId}</p> `.trim(); - const destMarker = Radar.ui.marker({ html: destContent, color: '#FF6F00' }) + const destMarker = Radar.ui.marker({ + color: '#FF6F00', + popup: { + html: destContent, + }, + }) .setLngLat(user.trip.destinationLocation.coordinates) .addTo(map); @@ -197,7 +206,7 @@

${location.latitude},${location.longitude}

`.trim(); - const userMarker = Radar.ui.marker({ html: userHtml }) + const userMarker = Radar.ui.marker({ popup: { html: userHtml }}) .setLngLat([location.longitude, location.latitude]) .addTo(map); @@ -208,7 +217,12 @@

${user.trip.destinationGeofenceExternalId}

`.trim(); - destMarker = Radar.ui.marker({ html: destHtml, color: '#FF6F00' }) + destMarker = Radar.ui.marker({ + color: '#FF6F00', + popup: { + html: destHtml, + }, + }) .setLngLat(user.trip.destinationLocation.coordinates) .addTo(map); } diff --git a/demo/views/track-a-user.hbs b/demo/views/track-a-user.hbs index b418c37..1d031f0 100644 --- a/demo/views/track-a-user.hbs +++ b/demo/views/track-a-user.hbs @@ -89,7 +89,7 @@ <p>${location.latitude},${location.longitude}</p> `.trim(); - const marker = Radar.ui.marker({ html }) + const marker = Radar.ui.marker({ popup: { html }}) .setLngLat([location.longitude, location.latitude]) .addTo(map); }); @@ -144,7 +144,7 @@

${location.latitude},${location.longitude}

`.trim(); - const marker = Radar.ui.marker({ html }) + const marker = Radar.ui.marker({ popup: { html }}) .setLngLat([location.longitude, location.latitude]) .addTo(map); diff --git a/src/types.ts b/src/types.ts index 228d713..f4a6306 100644 --- a/src/types.ts +++ b/src/types.ts @@ -463,22 +463,30 @@ export interface RadarMapOptions extends Omit { +const createImageElement = (options: ImageOptions) => { const element = document.createElement('img'); element.src = options.url!; @@ -30,13 +33,17 @@ const createImageElement = (options: RadarMarkerImage) => { return element; } +const defaultMarkerOptions: RadarMarkerOptions = { + color: '#000257', +}; + class RadarMarker extends maplibregl.Marker { _map!: RadarMap; - _image?: RadarMarkerImage; constructor(markerOptions: RadarMarkerOptions) { const maplibreOptions: maplibregl.MarkerOptions = Object.assign({}, defaultMarkerOptions); + // init MapLibre marker configs if (markerOptions.color) { maplibreOptions.color = markerOptions.color; } @@ -49,9 +56,8 @@ class RadarMarker extends maplibregl.Marker { super(maplibreOptions); - if (markerOptions.image) { - this._image = markerOptions.image; - + // handle marker images (Radar marker, or custom URL) + if (markerOptions.marker || markerOptions.url) { const originalElement = this._element.cloneNode(true); this._element.childNodes.forEach((child) => { child.remove(); @@ -67,8 +73,8 @@ class RadarMarker extends maplibregl.Marker { this._element.replaceChildren(...Array.from(originalElement.childNodes)); } - if (markerOptions.image.url) { - fetch(markerOptions.image.url) + if (markerOptions.url) { + fetch(markerOptions.url) .then(res => { if (res.status === 200) { res.blob() @@ -81,12 +87,12 @@ class RadarMarker extends maplibregl.Marker { .catch(onError) } - if (markerOptions.image.radarMarkerId) { + if (markerOptions.marker) { // fetch custom marker Http.request({ method: 'GET', version: 'maps', - path: `markers/${markerOptions.image.radarMarkerId}`, + path: `markers/${markerOptions.marker}`, responseType: 'blob', }) .then(({ data }) => onSuccess(data)) @@ -96,10 +102,12 @@ class RadarMarker extends maplibregl.Marker { // handle deprecated popup options if (markerOptions.text) { + Logger.warn('marker option "text" is deprecated, and will be removed in a future version. Use "popup.text".'); markerOptions.popup = markerOptions.popup || {}; markerOptions.popup!.text = markerOptions.text; } if (markerOptions.html) { + Logger.warn('marker option "html" is deprecated, and will be removed in a future version. Use "popup.html".'); markerOptions.popup = markerOptions.popup || {}; markerOptions.popup!.html = markerOptions.html; } @@ -114,6 +122,9 @@ class RadarMarker extends maplibregl.Marker { if (markerOptions.popup.html) { popup.setHTML(markerOptions.popup.html); } + if (markerOptions.popup.element) { + popup.setDOMContent(markerOptions.popup.element); + } this.setPopup(popup); }