|
| 1 | +import { useEffect, useRef, RefObject } from "react"; |
| 2 | +import { Map } from "mapbox-gl"; |
| 3 | +import { initMap } from "../utils"; |
| 4 | + |
| 5 | +export const useMap = (container: RefObject<HTMLDivElement>) => { |
| 6 | + const mapInitRef = useRef<Map | null>(null); |
| 7 | + |
| 8 | + useEffect(() => { |
| 9 | + if (container.current) { |
| 10 | + mapInitRef.current = initMap( |
| 11 | + container.current, |
| 12 | + [-100.31019063199852, 25.66901932031443] |
| 13 | + ); |
| 14 | + } |
| 15 | + }, []); |
| 16 | + |
| 17 | + // useEffect(() => { |
| 18 | + // if (!container.current) return; |
| 19 | + |
| 20 | + // mapInitRef.current.on("style.load", () => { |
| 21 | + // mapInitRef.current.setFog({}); // Set the default atmosphere style |
| 22 | + // }); |
| 23 | + |
| 24 | + // // At low zooms, complete a revolution every two minutes. |
| 25 | + // const secondsPerRevolution = 120; |
| 26 | + // // Above zoom level 5, do not rotate. |
| 27 | + // const maxSpinZoom = 5; |
| 28 | + // // Rotate at intermediate speeds between zoom levels 3 and 5. |
| 29 | + // const slowSpinZoom = 3; |
| 30 | + |
| 31 | + // function spinGlobe() { |
| 32 | + // const zoom = mapInitRef.current && mapInitRef.current.getZoom(); |
| 33 | + // if (zoom && zoom < maxSpinZoom) { |
| 34 | + // let distancePerSecond = 360 / secondsPerRevolution; |
| 35 | + // if (zoom > slowSpinZoom) { |
| 36 | + // // Slow spinning at higher zooms |
| 37 | + // const zoomDif = (maxSpinZoom - zoom) / (maxSpinZoom - slowSpinZoom); |
| 38 | + // distancePerSecond *= zoomDif; |
| 39 | + // } |
| 40 | + // const center = mapInitRef.current && mapInitRef.current.getCenter(); |
| 41 | + // if (center) { |
| 42 | + // center.lng -= distancePerSecond; |
| 43 | + // // Smoothly animate the map over one second. |
| 44 | + // // When this animation is complete, it calls a 'moveend' event. |
| 45 | + // mapInitRef.current && |
| 46 | + // mapInitRef.current.easeTo({ |
| 47 | + // center, |
| 48 | + // duration: 1000, |
| 49 | + // easing: (n) => n, |
| 50 | + // }); |
| 51 | + // } |
| 52 | + // } |
| 53 | + // } |
| 54 | + |
| 55 | + // // When animation is complete, start spinning if there is no ongoing interaction |
| 56 | + // mapInitRef.current.on("moveend", () => { |
| 57 | + // spinGlobe(); |
| 58 | + // }); |
| 59 | + |
| 60 | + // spinGlobe(); |
| 61 | + // }, []); |
| 62 | + |
| 63 | + // useEffect(() => { |
| 64 | + // mapInitRef.current && mapInitRef.current.on('dblclick', ({ lngLat }) => generateNewMarker({ map: mapInitRef.current!, ...lngLat })) |
| 65 | + |
| 66 | + // return () => { mapInitRef.current?.off('dblclick', generateNewMarker) } |
| 67 | + // }, []) |
| 68 | + |
| 69 | + // useEffect(() => { |
| 70 | + // mapInitRef.current && mapInitRef.current.on('load', () => generateNewMarker({ map: mapInitRef.current!, ...mapInitRef.current!.getCenter() })) |
| 71 | + |
| 72 | + // return () => { mapInitRef.current?.off('load', generateNewMarker) } |
| 73 | + // }, []) |
| 74 | +}; |
0 commit comments