From a93324379ae49cfb1a6e53dc6ce5ff77c41e908d Mon Sep 17 00:00:00 2001
From: Yuval Adam <_@yuv.al>
Date: Mon, 29 Aug 2022 17:45:00 +0300
Subject: Store lng, lat and zoom state, fix ref type

---
 src/Map.tsx | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

(limited to 'src/Map.tsx')

diff --git a/src/Map.tsx b/src/Map.tsx
index 76b1f11..032b6dd 100644
--- a/src/Map.tsx
+++ b/src/Map.tsx
@@ -17,9 +17,10 @@ interface MapProps {
 
 function Map({ slider, images }: MapProps) {
   const mapContainer = useRef(null);
-  const map = useRef(null);
-  const [center] = useState<mapboxgl.LngLatLike>([35, 31.9]);
-  const [zoom] = useState([6.3]);
+  const map = useRef<mapboxgl.Map>(null);
+  const [lng, setLng] = useState(35);
+  const [lat, setLat] = useState(31.9);
+  const [zoom, setZoom] = useState(6.3);
 
   useEffect(() => {
     if (map.current) return; // initialize map only once
@@ -27,14 +28,23 @@ function Map({ slider, images }: MapProps) {
       accessToken: MAPBOX_ACCESS_TOKEN,
       container: mapContainer.current,
       style: "mapbox://styles/mapbox/dark-v9",
-      center,
-      zoom,
+      center: [lng, lat],
+      zoom: zoom,
       minZoom: 5,
       maxZoom: 10,
       hash: false,
     });
   });
 
+  useEffect(() => {
+    if (!map.current) return; // wait for map to initialize
+    map.current.on('move', () => {
+      setLng(map.current.getCenter().lng.toFixed(4));
+      setLat(map.current.getCenter().lat.toFixed(4));
+      setZoom(map.current.getZoom().toFixed(2));
+    });
+  });
+
   return (
     <div>
       <div ref={mapContainer} className="map-container" style={{
-- 
cgit v1.3.1

