Skip to content

Commit

Permalink
add fitBoundsToGeoJson
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Jan 7, 2024
1 parent d2ec445 commit 1ce7436
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
32 changes: 12 additions & 20 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { TridentMapsStyle } from "@/types/TridentMaps";
import { useLocalStorage } from "@/hooks/localStorage";
import { FloatingChatButton } from "@/components/FloatingActionButton";
import { MapStyleSelector } from "@/components/MapStyleSelector";
import { fitBoundsToGeoJson } from "@/utils/map/fitBoundsToGeoJson";

const greetings = `Hello! I'm TRIDENT, interactive Smart Maps assistant. Could you indicate me the areas and themes you want to see as the map?`;

Expand All @@ -40,12 +41,15 @@ export default function Home() {

// floating chat button state
const [showingFloatingChat, setShowingFloatingChat] = useState(true);
const onChangeFloatingChatButton = useCallback((showing: boolean) => {
setShowingFloatingChat(showing);
if (showing) {
scrollToBottom();
}
}, [scrollToBottom]);
const onChangeFloatingChatButton = useCallback(
(showing: boolean) => {
setShowingFloatingChat(showing);
if (showing) {
scrollToBottom();
}
},
[scrollToBottom]
);

// maps ref and state
const mapRef = useRef<MapRef | null>(null);
Expand Down Expand Up @@ -81,9 +85,6 @@ export default function Home() {
.flat(),
};

// bounding box of all everything
const [minLng, minLat, maxLng, maxLat] = turf.bbox(everything);

// padding of the map
let padding = {
top: 40,
Expand Down Expand Up @@ -114,16 +115,7 @@ export default function Home() {
}
}

mapRef.current.fitBounds(
[
[minLng, minLat],
[maxLng, maxLat],
],
{
padding: padding,
duration: 1000,
}
);
fitBoundsToGeoJson(mapRef, everything, padding);
} catch (error) {
console.error(error);
}
Expand Down Expand Up @@ -423,7 +415,7 @@ export default function Home() {
</div>
);
})}
<div style={{ height: '1px' }} ref={dialogueEndRef} />
<div style={{ height: "1px" }} ref={dialogueEndRef} />
</div>
<TextInput
disabled={responding || lazyInserting || mapping}
Expand Down
23 changes: 23 additions & 0 deletions src/utils/map/fitBoundsToGeoJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MapRef, PaddingOptions } from "react-map-gl";
import * as turf from "@turf/turf";
import { MutableRefObject } from "react";

export const fitBoundsToGeoJson = (
mapRef: MutableRefObject<MapRef | null>,
geoJson: GeoJSON.FeatureCollection,
padding?: PaddingOptions
) => {
if (!mapRef || !mapRef.current) return;

const [minLng, minLat, maxLng, maxLat] = turf.bbox(geoJson);
mapRef.current.fitBounds(
[
[minLng, minLat],
[maxLng, maxLat],
],
{
padding: padding,
duration: 1000,
}
);
};
9 changes: 0 additions & 9 deletions src/utils/scrollToBotton.ts

This file was deleted.

1 comment on commit 1ce7436

@vercel
Copy link

@vercel vercel bot commented on 1ce7436 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.