Skip to content

Commit

Permalink
refactor: 사용자 위치 조회 로직 수정 (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
semnil5202 authored Oct 17, 2023
1 parent a28aaec commit 9ee045a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const CurrentLocationIcon = styled(CurrentLocation)<{
z-index: 10;
opacity: 0.85;
filter: ${({ $isUsingUserLocation }) =>
!$isUsingUserLocation && 'brightness(0.6)'};
$isUsingUserLocation && 'brightness(0.6)'};
@media (max-width: 1036px) {
bottom: 8%;
Expand Down
28 changes: 23 additions & 5 deletions frontend/src/hooks/useGeolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@ const useGeoLocation = (mapInstance: TMap | null) => {
coordinates: { lat: '', lng: '' },
});
const [userMarker, setUserMarker] = useState<Marker | null>(null);
const [isUsingUserLocation, setIsUsingUserLocation] = useState<boolean>(true);
const [isUsingUserLocation, setIsUsingUserLocation] =
useState<boolean>(false);

const removeUserMarker = () => {
if (watchPositionId.current) {
console.log('clear', watchPositionId.current);
navigator.geolocation.clearWatch(watchPositionId.current);
watchPositionId.current = null;
setLocation({
loaded: false,
coordinates: { lat: '', lng: '' },
});
}

userMarker?.setMap(null);
setUserMarker(null);
if (userMarker) {
userMarker.setMap(null);
setUserMarker(null);
}
};

const createUserMarkerWithZoomingMap = () => {
if (!mapInstance) return;

if (!isUsingUserLocation) {
if (isUsingUserLocation) {
removeUserMarker();
return;
}
Expand All @@ -55,7 +64,7 @@ const useGeoLocation = (mapInstance: TMap | null) => {
};

const toggleUsingUserLocation = () => {
if (location.coordinates.lat === '') return;
console.log(watchPositionId.current, 'ref');

setIsUsingUserLocation((prev) => !prev);
createUserMarkerWithZoomingMap();
Expand Down Expand Up @@ -86,19 +95,28 @@ const useGeoLocation = (mapInstance: TMap | null) => {
};

const requestUserLocation = () => {
console.log('request');
if (!('geolocation' in navigator)) {
onError({ code: 0, message: 'Geolocation not supported' });
}

showToast('info', '위치 정보를 불러오는 중입니다.');

if (isUsingUserLocation) {
showToast('info', '잠시 후 다시 시도해주세요.');
return;
}

watchPositionId.current = navigator.geolocation.watchPosition(
onSuccess,
onError,
);
};

useEffect(() => {
if (location.coordinates.lat === '') return;

console.log('useEffect');
toggleUsingUserLocation();
}, [location]);

Expand Down

0 comments on commit 9ee045a

Please sign in to comment.