diff --git a/src/ui/RadarMap.ts b/src/ui/RadarMap.ts index 010d4d7..80e3ce6 100644 --- a/src/ui/RadarMap.ts +++ b/src/ui/RadarMap.ts @@ -26,6 +26,10 @@ const defaultMaplibreOptions: Partial = { maplibreLogo: false, }; +const defaultFitMarkersOptions: maplibregl.FitBoundsOptions = { + padding: 50, +}; + const createStyleURL = (options: RadarOptions, mapOptions: RadarMapOptions) => { let url = `${options.host}/maps/styles/${mapOptions.style}?publishableKey=${options.publishableKey}` if (mapOptions.language) { @@ -140,6 +144,22 @@ class RadarMap extends maplibregl.Map { getMarkers(): RadarMarker[] { return this._markers; } + + fitToMarkers(fitBoundsOptions: maplibregl.FitBoundsOptions = {}, overrideMarkers?: RadarMarker[]) { + const markers = overrideMarkers || this.getMarkers(); + + if (markers.length === 0) { + return; + } + + const bounds = new maplibregl.LngLatBounds(); + markers.forEach((marker) => { + bounds.extend(marker.getLngLat()); + }); + + const options = Object.assign(defaultFitMarkersOptions, fitBoundsOptions); + this.fitBounds(bounds, options); + } }; export default RadarMap;