diff --git a/package-lock.json b/package-lock.json index b2fa69b..37b3c02 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,8 @@ "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "@turf/bbox": "^6.5.0", + "@turf/center": "^6.5.0", + "@turf/helpers": "^6.5.0", "@types/jest": "^27.5.2", "@types/mapbox-gl": "^2.7.11", "@types/node": "^16.18.7", @@ -3519,6 +3521,18 @@ "url": "https://opencollective.com/turf" } }, + "node_modules/@turf/center": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/center/-/center-6.5.0.tgz", + "integrity": "sha512-T8KtMTfSATWcAX088rEDKjyvQCBkUsLnK/Txb6/8WUXIeOZyHu42G7MkdkHRoHtwieLdduDdmPLFyTdG5/e7ZQ==", + "dependencies": { + "@turf/bbox": "^6.5.0", + "@turf/helpers": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, "node_modules/@turf/helpers": { "version": "6.5.0", "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.5.0.tgz", @@ -17931,6 +17945,15 @@ "@turf/meta": "^6.5.0" } }, + "@turf/center": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/center/-/center-6.5.0.tgz", + "integrity": "sha512-T8KtMTfSATWcAX088rEDKjyvQCBkUsLnK/Txb6/8WUXIeOZyHu42G7MkdkHRoHtwieLdduDdmPLFyTdG5/e7ZQ==", + "requires": { + "@turf/bbox": "^6.5.0", + "@turf/helpers": "^6.5.0" + } + }, "@turf/helpers": { "version": "6.5.0", "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.5.0.tgz", diff --git a/package.json b/package.json index f114950..e2050b9 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "@turf/bbox": "^6.5.0", + "@turf/center": "^6.5.0", + "@turf/helpers": "^6.5.0", "@types/jest": "^27.5.2", "@types/mapbox-gl": "^2.7.11", "@types/node": "^16.18.7", diff --git a/src/contexts/MapContext.tsx b/src/contexts/MapContext.tsx index cb6d7d0..abaad99 100644 --- a/src/contexts/MapContext.tsx +++ b/src/contexts/MapContext.tsx @@ -1,15 +1,23 @@ import { createContext, useContext, useState, ReactNode } from 'react' -import type { LngLatBoundsLike } from 'mapbox-gl' +import type { LngLatLike, LngLatBoundsLike } from 'mapbox-gl' import type { GeoJsonProperties, Feature, Point } from 'geojson' + import bbox from '@turf/bbox' +import center from '@turf/center' +import {featureCollection} from '@turf/helpers' + +type MapBounds = { + bounds: LngLatBoundsLike, + center: LngLatLike +} interface MapConfig { points: Array> stashedPoints: Array> | undefined setStashedPoints: (points: Array> | undefined) => void updateStoryPoints: (newPoints: Array>) => void - bounds?: LngLatBoundsLike + bounds?: MapBounds } const MapContext = createContext({ @@ -24,15 +32,15 @@ export const MapContextProvider = ({ children, initialPoints }: {children: React const [points, setPoints] = useState>>(initialPoints) const [stashedPoints, setStashedPoints] = useState>>() - const [bounds, setBounds] = useState() + const [bounds, setBounds] = useState() function updateStoryPoints(newPoints: Array>) { setPoints(newPoints) - const bounds = { - type: 'FeatureCollection', - features: newPoints - } - setBounds(bbox(bounds) as LngLatBoundsLike) + const bounds = featureCollection(newPoints) + setBounds({ + bounds: bbox(bounds) as LngLatBoundsLike, + center: center(bounds).geometry.coordinates as LngLatLike + }) } return ( diff --git a/src/pages/community/components/Map/index.tsx b/src/pages/community/components/Map/index.tsx index 2906c18..5d938f2 100644 --- a/src/pages/community/components/Map/index.tsx +++ b/src/pages/community/components/Map/index.tsx @@ -152,7 +152,7 @@ export default function Map({config}: {config: MapData}) { if (!mapRef.current) return const map = mapRef.current if (bounds) { - map.fitBounds(bounds, {padding: 50, duration: 2000.0, maxZoom: 12}) + map.fitBounds(bounds.bounds, {center: bounds.center, padding: 50, duration: 2000.0, maxZoom: 12}) } }, [bounds])