본문 바로가기

What I Learnd

서울시 실시간 날씨 받아오기 Open Weather Map API

오픈웨더앱에서 날씨 api 를 받아오기로

https://openweathermap.org/

 

Сurrent weather and forecast - OpenWeatherMap

Access current weather data for any location on Earth including over 200,000 cities! The data is frequently updated based on the global and local weather models, satellites, radars and a vast network of weather stations. how to obtain APIs (subscriptions w

openweathermap.org

 

  1. 회원가입
  2. 자동으로 내 api 주소 만들어짐 (MyAPI가서 확인ㄱㄱ)
  3. 원하는 api 골라서 (나는 Current Weather Data로 골라봄)
  4. 내가 필요한 지역에 맞게 코드 수정해주고 내API키 넣기
  5. fetch로 받아오기
https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}

*위치경도로 지역 넣어줄 수도 있고 이름으로 넣어줄 수도 있는데, 나는 위치경도를 구글링해서 적용해봤지만 안되길래 이름으로 검색해서 넣어줌! 

나는 여기서 지역, 온도, 날씨 이렇게 3가지만 가져오고 싶음

 

완성코드 타란!

fetch("https://api.openweathermap.org/data/2.5/weather?q=Seoul,kr&appid={본인키넘버}")
                .then((response) => response.json())
                .then((data) => {
                    $('#names-q1').empty();
                    let main = data['weather'][0]['main'];
                    let temp = data['main']['temp'];
                    let location = data['name'];

                    let temp_html = `
                        <li>${location}</li>
                        <li>${temp}</li>
                        <li>${main}</li>
                        `;
                    $('#names-q1').append(temp_html);
                })