Skip to content

Commit

Permalink
Separate out getting zoom level from setCenter
Browse files Browse the repository at this point in the history
  • Loading branch information
mwpark2014 committed Apr 29, 2023
1 parent 1ef7682 commit d41229b
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions client/src/pages/Map/MapboxManager.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d41229b

Please sign in to comment.