From d41229b14ebcd435e2b8a53417919bf8060ca7e2 Mon Sep 17 00:00:00 2001 From: Mason Park Date: Sat, 29 Apr 2023 14:33:38 -0700 Subject: [PATCH] Separate out getting zoom level from setCenter --- client/src/pages/Map/MapboxManager.js | 55 +++++++++++++-------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/client/src/pages/Map/MapboxManager.js b/client/src/pages/Map/MapboxManager.js index f7253481..fa5f6d8c 100644 --- a/client/src/pages/Map/MapboxManager.js +++ b/client/src/pages/Map/MapboxManager.js @@ -1,7 +1,10 @@ import { REGION_TYPES } from '../../util/constants'; class MapboxManager { - // map is expected to be nullable. Mostly serves as a way to pass in a mock in tests + // @param {mapboxgl.Map} markerRef: a mapboxgl.Map() object + // See https://docs.mapbox.com/mapbox-gl-js/api/map/ + // map is expected to be nullable initially while map.js creates the mapboxgl.Map() object. + // Mostly serves as a way to pass in a mock in tests constructor(map) { this.map = map; } @@ -31,49 +34,43 @@ class MapboxManager { regionType = regionType[0]; } + const zoomLevel = this._getZoom(regionType); + if (zoomLevel) { + this.map.flyTo({ + center: coords, + zoom: zoomLevel, + }); + } else { + this.map.flyTo({ + center: coords, + }); + } + } + + _getZoom(regionType) { switch (regionType) { case REGION_TYPES.COUNTRY: - this.map.flyTo({ - center: coords, - zoom: 4.5, - }); - break; + return 4.5; case REGION_TYPES.REGION: - this.map.flyTo({ - center: coords, - zoom: 6, - }); - break; + return 6; case REGION_TYPES.PLACE: case REGION_TYPES.DISTRICT: case REGION_TYPES.POSTAL_CODE: - this.map.flyTo({ - center: coords, - zoom: 12, - }); - break; + return 12; case REGION_TYPES.NEIGHBORHOOD: case REGION_TYPES.LOCALITY: - this.map.flyTo({ - center: coords, - zoom: 14, - }); - break; + return 14; case REGION_TYPES.ADDRESS: case REGION_TYPES.LATLONG: case REGION_TYPES.POI: - this.map.flyTo({ - center: coords, - zoom: 17, - }); - break; + return 17; default: - this.map.flyTo({ - center: coords, - }); + return null; } } + // @param {RefObject} markerRef: ref to a mapboxgl.Marker() object + // See https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker#addto addMarkerRefToMap(markerRef) { if (!this.map) { return;