Skip to content

Commit 9b7e1d4

Browse files
committed
Fix bug in CityMapQuiz that caused wrong map bounds positioning
1 parent 300069f commit 9b7e1d4

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/components/CityMapQuiz.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { distance } from "fastest-levenshtein";
1111
import type { FeatureCollection } from "geojson";
1212
import "maplibre-gl/dist/maplibre-gl.css";
1313
import { useMemo, useRef, useState } from "react";
14-
import type { MapRef, StyleSpecification } from "react-map-gl/maplibre";
14+
import type { LngLatBoundsLike, MapRef, StyleSpecification } from "react-map-gl/maplibre";
1515
import { AttributionControl, Map as MaplibreMap } from "react-map-gl/maplibre";
1616
import { Mode } from "../enums";
1717
import { shuffle } from "../utils/ArrayUtils";
@@ -49,10 +49,12 @@ export default function CityMapQuiz({ data, datasetName, onResetGame }: Props) {
4949

5050
const currentFeatureId = featureIds[featureIds.length - 1];
5151
const currentFeature = data.features.find(item => item.id === currentFeatureId) || data;
52-
const [initialMinLng, initialMinLat, initialMaxLng, initialMaxLat] = bbox(transformScale(currentFeature, 0.65));
53-
const [minLng, minLat, maxLng, maxLat] = bbox(transformScale(currentFeature, 2));
54-
mapRef.current?.fitBounds([initialMinLng, initialMinLat, initialMaxLng, initialMaxLat], { padding: 0, duration: 1000 });
55-
mapRef.current?.getMap().setMaxBounds([minLng, minLat, maxLng, maxLat]);
52+
53+
// setMaxBounds must be called before fitBounds
54+
const maxBounds = bbox(transformScale(currentFeature, 2)) as LngLatBoundsLike;
55+
mapRef.current?.getMap().setMaxBounds(maxBounds);
56+
const initialBounds = bbox(transformScale(currentFeature, 0.65)) as LngLatBoundsLike;
57+
mapRef.current?.fitBounds(initialBounds, { padding: 0, duration: 1500 });
5658

5759
const handleTextInput = (input: string) => {
5860
setUserGuess(input);
@@ -79,9 +81,9 @@ export default function CityMapQuiz({ data, datasetName, onResetGame }: Props) {
7981
<div className="h-screen flex items-center justify-center">
8082
<MaplibreMap
8183
initialViewState={{
82-
bounds: [initialMinLng, initialMinLat, initialMaxLng, initialMaxLat]
84+
bounds: initialBounds
8385
}}
84-
maxBounds={[minLng, minLat, maxLng, maxLat]}
86+
maxBounds={maxBounds}
8587
doubleClickZoom={false}
8688
dragRotate={false}
8789
touchPitch={false}

0 commit comments

Comments
 (0)