Skip to content

Commit

Permalink
Merge pull request #20 from robertd2000/18-ls
Browse files Browse the repository at this point in the history
18 ls
  • Loading branch information
robertd2000 authored Mar 28, 2024
2 parents b2508ba + 280af2b commit e62ba9d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/components/Map/MainMap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const MainMap = memo(() => {
<RMap
className={style.mainMap}
initial={view}
minZoom={4}
maxZoom={8}
minZoom={8}
maxZoom={18}
projection={"EPSG:3857"}
view={[view, onSetView]}
ref={mapRef}
Expand Down
13 changes: 13 additions & 0 deletions src/components/Map/MainMap/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { localStorageController } from "@/utils/localStorage";
import { fromLonLat } from "ol/proj";

export const getInitialData = () => {
const lon = localStorageController.getItem("lon");
const lat = localStorageController.getItem("lat");
const zoom = localStorageController.getItem("zoom") as number;

return {
center: fromLonLat([(lon as number) || 2.364, (lat as number) || 48.82]),
zoom: +zoom,
};
};
8 changes: 7 additions & 1 deletion src/hooks/map/useMapCoords.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { useSearchParams } from "react-router-dom";
import { toLonLat } from "ol/proj";
import { RMap } from "rlayers";
import { RView } from "node_modules/rlayers/RMap";
import { initialMapData } from "../../components/Map/MainMap/constants/initial";
import { localStorageController } from "@/utils/localStorage";
import { getInitialData } from "@/components/Map/MainMap/utils";

const initialMapData = getInitialData();

export const useMapCoords = () => {
const mapRef = useRef<RMap>(null);
Expand All @@ -23,6 +26,9 @@ export const useMapCoords = () => {
searchParams.set("resolution", `${view.resolution}`);

setSearchParams(searchParams);
localStorageController.setItem("lon", lon.toString());
localStorageController.setItem("lat", lat.toString());
localStorageController.setItem("zoom", view.zoom.toString());
},
[searchParams]
);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/localStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const localStorageController = {
setItem: (k: string, v: string): void => localStorage.setItem(k, v),
getItem: <V>(k: string): V | null => {
const v = localStorage.getItem(k);
return v ? (JSON.parse(v) as V) : null;
},
removeItem: (k: string): void => localStorage.removeItem(k),
removeAll: (): void => localStorage.clear(),
checkKey: (k: string): boolean => Object.keys(localStorage).includes(k),
};

0 comments on commit e62ba9d

Please sign in to comment.