-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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, | ||
); | ||
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; | ||
|
@@ -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 { | ||
|
@@ -71,4 +106,13 @@ const MapFlex = styled(Flex)` | |
} | ||
`; | ||
|
||
const CurrentLocationIcon = styled(CurrentLocation)` | ||
position: absolute; | ||
bottom: 10px; | ||
left: 10px; | ||
width: 24px; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하! 왼쪽 하단으로 붙여주셨군요! 음 혹시 오른쪽 하단은 어떠신가요?! 오른손잡이들의 세상이잖아요?! ㅋㅋㅋㅋㅋㅋㅋㅋ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분도 제가 바꿨습니다 ㅋㅋㅋㅋㅋ |
||
height: 24px; | ||
z-index: 10; | ||
`; | ||
|
||
export default Map; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기 console 부분만 toast로 바꾸면 좋을 것 같아요 !!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제가 바꿨습니다~~