[바닐라 JS] 현재위치정보 가져오기

이지수·2021년 7월 9일
0
post-thumbnail

getCurrentPosition()

두개의 인수가 필요하다. 한개는 모든게 잘 됐을 때 실행될 함수
나머지 한개는 에러가 발생했을 때 실행 될 함수

navigator.geolocation.getCurrentPosition()

function onGeoOk(position){
  const lat = position.coords.latitude;
  const lng = position.coords.longitude;
  console.log("you live in", lat, lng);
}
function onGeoError(){
  alert("Can't find you. No weather for you.")
}

navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError )

위의 간단한 코드로 현재 위치 정보를 위도와 경도로 알 수 있다.
이를 활용하여 날씨정보를 알아볼 예정이다.

reference

노마드코더

profile
The only thing worse than starting something and failing...is not starting something

0개의 댓글