From b839008f3b1641ebac64419791bc9b9181e576d9 Mon Sep 17 00:00:00 2001 From: Jason Liu Date: Wed, 15 May 2024 13:25:12 -0400 Subject: [PATCH] `RadarMap.fitToMarkers` class method (#156) --- src/ui/RadarMap.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/ui/RadarMap.ts b/src/ui/RadarMap.ts index e5e32fb1..76491f08 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, style: string = DEFAULT_STYLE) => ( `${options.host}/maps/styles/${style}?publishableKey=${options.publishableKey}` ); @@ -135,6 +139,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;