본문 바로가기

Study/Web

[JS] 바닐라 JS | #8.0~8.2) weather API 사용해보기

반응형

#8.0 Geolocation

아래의 코드를 입력하면 내가 있는 곳의 위도와 경도를 콘솔에 띄울 수 이따따ㅏ따. 루꼣디스뷰리.

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);

#8.1 Weather API

https://openweathermap.org/api 요기에서 API 가져올 수 있다.

API는 다른 서버와 이야기할 수 있는 방법이다.

 

units도 조정할 수 있다. url 뒤에 &units=metric를 추가하면 된다.

 

fetch는 promise다.

흠 로컬에서는 잘 된다.

반응형