const geolocation = () => {
navigator.geolocation.getCurrentPosition(
(성공) => 성공,
(실패) => 실패
);
};
예시 코드를 이러며, 성공에는 latitude와 longtitude가 담겨있고, 실패에는 error의 code와 설명인 message가 담겨온다.
근데 이게 바로 응답이 오는게 아니라 좀 기다려야하다 보니 다른데서 이를 가져다 활용하려면 Promise로 만들어서 await을 해줘야한다.
export const getLocation = async () => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve(getLatLot(position)),
(error) => reject(error)
);
});
};
나는 이런식으로 만들어서 가져다 사용했다.
참고로 한국의 위경도 범위는
극동: 경상북도 울릉군의 독도 동단 동경 131° 52′20" → 131.87222222
극서: 평안북도 용천군 신도면 마안도 서단 동경 124° 11′45" → 124.19583333
극남: 제주도 남제주군 대정읍 마라도 남단 북위 33° 06′40" → 33.11111111
극북: 함경북도 온성군 남양면 북단 북위 43° 00′35" → 43.00972222
이다.
!(latitude >= 33.11111111 && latitude <= 43.00972222) ||
!(longitude >= 124.19583333 && longitude <= 131.87222222)
좌표를 통해서 국내외 판별
좌표를 통해서 국내외 판별하는 과정을 살펴봅니다.
velog.io
Geolocation 공싯 사이트
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API
Geolocation API - Web APIs | MDN
The Geolocation API allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information.
developer.mozilla.org
'코딩 > API' 카테고리의 다른 글
http 상태 코드 (1) | 2024.09.20 |
---|---|
PRG: Post/Redirect/Get (0) | 2024.09.10 |
날씨 가져오기 Openweather (0) | 2024.03.31 |
API연습, 설명서 (0) | 2024.03.02 |