Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maintains zoom level and centers active pins #2219 #2236

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions client/src/hooks/useMapbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { defaultViewport } from "helpers/Configuration";
import { useRef } from "react";
import { useMap } from "react-map-gl";
import { useListPanel } from "../appReducer";
Expand Down Expand Up @@ -28,12 +27,28 @@ export const useMapbox = () => {
if (!mapbox.default) {
return;
}

// gets the user's current zoom level
const currentZoom = mapbox.default.getZoom();

// calculates longitude offset according to zoom level for panel open desktop
const baseLongOffset = 0.08;
const longitudeOffset = baseLongOffset * Math.pow(2, 11 - currentZoom);

// calculates latitude offset for mobile according to zoom level and takes screen height into account
const baseLatOffset = 0.05;
const screenHeight = window.innerHeight;
const referenceHeight = 800;
const screenHeightFactor = screenHeight / referenceHeight;
const latitudeOffset =
baseLatOffset * Math.pow(2, 11 - currentZoom) * screenHeightFactor;

mapbox.default.flyTo({
center: [
isListPanelOpen && isDesktop ? longitude - 0.08 : longitude,
isMobile ? latitude - 0.04 : latitude,
isListPanelOpen && isDesktop ? longitude - longitudeOffset : longitude,
isMobile ? latitude - latitudeOffset : latitude,
],
zoom: defaultViewport.zoom,
zoom: currentZoom,
duration: 2000,
});
};
Expand Down