Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] Feat/#588 현재 위치 이동 버튼 #589

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/src/assets/currentLocationBtn.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 52 additions & 8 deletions frontend/src/components/Map/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useContext, useLayoutEffect, useRef, useState } from 'react';
import { styled } from 'styled-components';

import CurrentLocation from '../../assets/currentLocationBtn.svg';
import { LayoutWidthContext } from '../../context/LayoutWidthContext';
import { MarkerContext } from '../../context/MarkerContext';
import useAnimateClickedPin from '../../hooks/useAnimateClickedPin';
import useClickedCoordinate from '../../hooks/useClickedCoordinate';
import useFocusToMarker from '../../hooks/useFocusToMarkers';
import useGeoLocation from '../../hooks/useGeolocation';
import useMapClick from '../../hooks/useMapClick';
import useUpdateCoordinates from '../../hooks/useUpdateCoordinates';
import Flex from '../common/Flex';
Expand All @@ -18,6 +20,32 @@ function Map() {
const [mapInstance, setMapInstance] = useState<TMap | null>(null);

const mapContainer = useRef(null);
const location = useGeoLocation();

const handleCurrentLocationClick = () => {
if (!location.loaded) {
console.log('위치 정보를 불러오는 중입니다...');
return;
}

if (location.error) {
console.error(
'위치 정보를 불러오는데 실패했습니다:',
location.error.message,
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 console 부분만 toast로 바꾸면 좋을 것 같아요 !!!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 바꿨습니다~~

return;
}

if (mapInstance) {
mapInstance.setCenter(
new Tmapv3.LatLng(
Number(location.coordinates.lat),
Number(location.coordinates.lng),
),
);
mapInstance.setZoom(15);
}
};

useLayoutEffect(() => {
if (!Tmapv3 || !mapContainer.current) return;
Expand Down Expand Up @@ -47,17 +75,24 @@ function Map() {
useAnimateClickedPin(mapInstance, markers);

return (
<MapFlex
aria-label="괜찮을지도 지도 이미지"
flex="1"
id="map"
ref={mapContainer}
height="calc(var(--vh, 1vh) * 100)"
$minWidth={width}
/>
<MapContainer>
<MapFlex
aria-label="괜찮을지도 지도 이미지"
flex="1"
id="map"
ref={mapContainer}
height="calc(var(--vh, 1vh) * 100)"
$minWidth={width}
/>
<CurrentLocationIcon onClick={handleCurrentLocationClick} />
</MapContainer>
);
}

const MapContainer = styled.div`
position: relative;
`;

const MapFlex = styled(Flex)`
& {
canvas {
Expand All @@ -71,4 +106,13 @@ const MapFlex = styled(Flex)`
}
`;

const CurrentLocationIcon = styled(CurrentLocation)`
position: absolute;
bottom: 10px;
left: 10px;
width: 24px;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하! 왼쪽 하단으로 붙여주셨군요! 음 혹시 오른쪽 하단은 어떠신가요?! 오른손잡이들의 세상이잖아요?! ㅋㅋㅋㅋㅋㅋㅋㅋ

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분도 제가 바꿨습니다 ㅋㅋㅋㅋㅋ

height: 24px;
z-index: 10;
`;

export default Map;
2 changes: 1 addition & 1 deletion frontend/src/components/common/Input/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import styled from 'styled-components';

import { getPoiApi } from '../../../apis/getPoiApi';
import { Poi } from '../../../types/Poi';
import Input from '.';
import Text from '../Text';
import Input from '.';

interface AutocompleteProps {
defaultValue?: string;
Expand Down
43 changes: 19 additions & 24 deletions frontend/src/hooks/useGeolocation.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,46 @@
import { useEffect, useState } from 'react';

interface locationType {
type GeoLocationState = {
loaded: boolean;
currentLocation?: { latitude: number; longitude: number };
coordinates: { lat: string | number; lng: string | number };
error?: { code: number; message: string };
}
};

const useGeolocation = () => {
const [location, setLocation] = useState<locationType>({
const useGeoLocation = () => {
const [location, setLocation] = useState<GeoLocationState>({
loaded: false,
currentLocation: { latitude: 0, longitude: 0 },
coordinates: { lat: '', lng: '' },
});

// 성공에 대한 로직
const onSuccess = (location: {
coords: { latitude: number; longitude: number };
}) => {
const onSuccess = (location: any) => {
setLocation({
loaded: true,
currentLocation: {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
coordinates: {
lat: location.coords.latitude,
lng: location.coords.longitude,
},
});
};

// 에러에 대한 로직
const onError = (error: { code: number; message: string }) => {
const onError = (error: any) => {
setLocation({
loaded: true,
error,
coordinates: { lat: '', lng: '' },
error: {
code: error.code,
message: error.message,
},
});
};

useEffect(() => {
// navigator 객체 안에 geolocation이 없다면
// 위치 정보가 없는 것.
if (!('geolocation' in navigator)) {
onError({
code: 0,
message: 'Geolocation not supported',
});
onError({ code: 0, message: 'Geolocation not supported' });
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}, [location]);
}, []);

return location;
};

export default useGeolocation;
export default useGeoLocation;