Skip to content

Commit

Permalink
fix: Use turf center to handle fitBounds on non-Mercator projections (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rudokemper authored Sep 26, 2023
1 parent 2a6d5da commit 6fa3069
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
7 changes: 6 additions & 1 deletion rails/app/javascript/components/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import Popup from "./Popup";
import mapboxgl from '!mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

import center from '@turf/center'
import bboxPolygon from '@turf/bbox-polygon'

const STORY_POINTS_LAYER_ID = "ts-points-layer";
const STORY_POINTS_DATA_SOURCE = "ts-points-data";

Expand Down Expand Up @@ -209,7 +212,9 @@ export default class Map extends Component {
) {
const { bounds, ...frameOptions } = this.props.framedView;
if (bounds) {
this.map.fitBounds(bounds, { padding: 50, duration: 2000.0, ...frameOptions });
const bboxPoly = bboxPolygon(bounds);
const centerPoint = center(bboxPoly).geometry.coordinates;
this.map.fitBounds(bounds, { center: centerPoint, padding: 50, duration: 2000.0, maxZoom: 12, ...frameOptions });
} else {
this.map.easeTo({ duration: 2000.0, ...frameOptions });
}
Expand Down
6 changes: 4 additions & 2 deletions rails/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"@babel/core": "^7.20.12",
"@babel/preset-react": "^7.18.6",
"@rails/webpacker": "5.4.4",
"@turf/bbox": "^6.0.1",
"@turf/helpers": "^6.1.4",
"@turf/bbox": "^6.5.0",
"@turf/bbox-polygon": "^6.5.0",
"@turf/center": "^6.5.0",
"@turf/helpers": "^6.5.0",
"awesomplete": "^1.1.5",
"babel-jest": "24.0.0",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
Expand Down
19 changes: 17 additions & 2 deletions rails/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1445,15 +1445,30 @@
webpack-cli "^3.3.12"
webpack-sources "^1.4.3"

"@turf/bbox@^6.0.1":
"@turf/bbox-polygon@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/bbox-polygon/-/bbox-polygon-6.5.0.tgz#f18128b012eedfa860a521d8f2b3779cc0801032"
integrity sha512-+/r0NyL1lOG3zKZmmf6L8ommU07HliP4dgYToMoTxqzsWzyLjaj/OzgQ8rBmv703WJX+aS6yCmLuIhYqyufyuw==
dependencies:
"@turf/helpers" "^6.5.0"

"@turf/bbox@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/bbox/-/bbox-6.5.0.tgz#bec30a744019eae420dac9ea46fb75caa44d8dc5"
integrity sha512-RBbLaao5hXTYyyg577iuMtDB8ehxMlUqHEJiMs8jT1GHkFhr6sYre3lmLsPeYEi/ZKj5TP5tt7fkzNdJ4GIVyw==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/meta" "^6.5.0"

"@turf/helpers@^6.1.4", "@turf/helpers@^6.5.0":
"@turf/center@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/center/-/center-6.5.0.tgz#3bcb6bffcb8ba147430cfea84aabaed5dbdd4f07"
integrity sha512-T8KtMTfSATWcAX088rEDKjyvQCBkUsLnK/Txb6/8WUXIeOZyHu42G7MkdkHRoHtwieLdduDdmPLFyTdG5/e7ZQ==
dependencies:
"@turf/bbox" "^6.5.0"
"@turf/helpers" "^6.5.0"

"@turf/helpers@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-6.5.0.tgz#f79af094bd6b8ce7ed2bd3e089a8493ee6cae82e"
integrity sha512-VbI1dV5bLFzohYYdgqwikdMVpe7pJ9X3E+dlr425wa2/sMJqYDhTO++ec38/pcPvPE6oD9WEEeU3Xu3gza+VPw==
Expand Down

0 comments on commit 6fa3069

Please sign in to comment.