11import { Box } from '@chakra-ui/react' ;
2- import { Dispatch , SetStateAction , useEffect } from 'react' ;
2+ import { Dispatch , SetStateAction , useEffect , useState } from 'react' ;
33import { Container as MapDiv , NaverMap , useNavermaps } from 'react-naver-maps' ;
44import { useLocation } from 'react-router-dom' ;
55import getGardenType from '../utils/getGardenType' ;
66import GardensContainer from './GardensContainer' ;
77import MapSpinner from './MapSpinner' ;
88import MarkerCluster from './Marker/MarkerCluster' ;
99import MyMarker from './Marker/MyMarker' ;
10- import useGeolocation from '@/hooks/useGeolocation' ;
1110import { useGetMapGardens } from '@/services/gardens/query' ;
1211import useMapGardenDetailIdStore from '@/stores/useMapGardenDetailIdStore' ;
1312
@@ -20,7 +19,12 @@ interface MapComponentProps {
2019const MapComponent = ( { map, setMap, headerOption } : MapComponentProps ) => {
2120 const location = useLocation ( ) ;
2221 const navermaps = useNavermaps ( ) ;
23- const geolocation = useGeolocation ( ) ;
22+ // const geolocation = useGeolocation();
23+ const [ isCurrentLocationLoaded , setIsCurrentLocationLoaded ] = useState ( false ) ;
24+ const [ position , setPosition ] = useState ( {
25+ lat : 37.3595704 ,
26+ lng : 127.105399 ,
27+ } ) ;
2428 const gardenType = getGardenType ( headerOption ) ;
2529 const { gardenId } = useMapGardenDetailIdStore ( ) ;
2630 const { data : mapGardens , refetch } = useGetMapGardens ( gardenType , map ) ;
@@ -53,21 +57,31 @@ const MapComponent = ({ map, setMap, headerOption }: MapComponentProps) => {
5357 }
5458 } , [ map , refetch , headerOption ] ) ;
5559
56- let position = {
57- lat : 37.3595704 ,
58- lng : 127.105399 ,
59- } ;
60-
61- if ( geolocation . loaded && ! geolocation . error && geolocation . coordinates ) {
62- position = {
63- lat : geolocation . coordinates . lat ,
64- lng : geolocation . coordinates . lng ,
65- } ;
66- }
60+ // if (geolocation.loaded && !geolocation.error && geolocation.coordinates) {
61+ // position = {
62+ // lat: geolocation.coordinates.lat,
63+ // lng: geolocation.coordinates.lng,
64+ // };
65+ // }
6766
68- if ( ! geolocation . loaded ) {
69- return < MapSpinner /> ;
70- }
67+ useEffect ( ( ) => {
68+ if ( navigator . geolocation ) {
69+ navigator . geolocation . getCurrentPosition (
70+ ( position ) => {
71+ setPosition ( {
72+ lat : position . coords . latitude ,
73+ lng : position . coords . longitude ,
74+ } ) ;
75+ setIsCurrentLocationLoaded ( true ) ;
76+ } ,
77+ ( ) => {
78+ setIsCurrentLocationLoaded ( true ) ;
79+ } ,
80+ ) ;
81+ } else {
82+ setIsCurrentLocationLoaded ( true ) ;
83+ }
84+ } , [ ] ) ;
7185
7286 const getDefaultCenter = ( ) => {
7387 if ( gardenId && location . state && location . state . data ) {
@@ -80,6 +94,10 @@ const MapComponent = ({ map, setMap, headerOption }: MapComponentProps) => {
8094 return new navermaps . LatLng ( position . lat , position . lng ) ;
8195 } ;
8296
97+ if ( ! isCurrentLocationLoaded ) {
98+ return < MapSpinner /> ;
99+ }
100+
83101 return (
84102 < Box
85103 position = "relative"
@@ -93,7 +111,6 @@ const MapComponent = ({ map, setMap, headerOption }: MapComponentProps) => {
93111 map,
94112 } }
95113 />
96-
97114 < MapDiv style = { { width : '100%' , height : '100%' } } >
98115 < NaverMap
99116 ref = { setMap }
0 commit comments