-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 지도 위 현재 위치로 버튼 추가 * design: 현재 위치 아이콘 변경 및 위치 변경 * refactor: Toast로 사용자에게 위치 정보 상태를 명시 --------- Co-authored-by: semnil5202 <semnil5202@gmail.com>
- Loading branch information
1 parent
ff18a09
commit 45d395d
Showing
4 changed files
with
81 additions
and
33 deletions.
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
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; |