Back to feed
Renewal·마흔의 생활코딩

Cursor IDE_coding test01 — weather API

NS
normalstory
cover image

Building a simple HTML page that uses an open API to tell you today's weather. 

 

 

1. Create an HTML file.

2. Type "html" + Enter to set up the basic frame.

3. With the mouse, select a region between the body tags and click "Add to Chat".

 

 

4. In the Chat area, type in what you want to build.

 

 

5. Copy and paste the code.

6. Sign up for the site it pointed you to, get an API key, and put it in.

 

 

7. Run the code in a browser. 

 

 

8. Enter a more detailed prompt in the Chat screen, like the one below: 

Take the previous code and
1. Use CSS so that the screen shows 'Today's weather in Suwon: {weather info}' as a centered title, and make the map appear as a card occupying the center 60% of the screen.
2. Next to the words "weather info," show an owm-weather-icon, and add code that displays the weather info and the wind speed alongside it.

 

 

 

 

 


Full code

<!DOCTYPE html>
<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        #title {
            text-align: center;
            font-size: 2em;
            margin-top: 20px;
        }
        .map {
            height: 60vh;
            width: 60%;
            margin: 20px 0;
            border: 1px solid black;
        }
        #hourlyForecast, #dailyForecast {
            width: 60%;
            height: 60vh;
        }
        .ol-attribution{
            display: none;
        }
    </style>
</head>
<body>
    <div id="title"></div>
    <div id="map" class="map"></div>
    <div id="airPollution"></div>
    <canvas id="hourlyForecast"></canvas>
    <canvas id="dailyForecast"></canvas>
    <script>
        var apiKey = 'f770c934b0cb93b54e8c96cd14995682';
        var city = 'Suwon,KR';

        // 오늘의 날씨 정보 가져오기
        fetch('http://api.openweathermap.org/data/2.5/weather?q=' + city + '&appid=' + apiKey)
        .then(function(resp) { return resp.json() })
        .then(function(data) {
            var weatherIcon = "http://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
            document.getElementById('title').innerHTML = '오늘의 수원 날씨: <img src="' + weatherIcon + '"> ' + data.weather[0].main + ', 바람 세기: ' + data.wind.speed;
            var lat = data.coord.lat;
            var lon = data.coord.lon;

            // 대기 오염 정보 가져오기
            fetch('http://api.openweathermap.org/data/2.5/air_pollution?lat=' + lat + '&lon=' + lon + '&appid=' + apiKey)
            .then(function(resp) { return resp.json() })
            .then(function(data) {
                document.getElementById('airPollution').innerHTML = '대기 오염 지수: ' + data.list[0].main.aqi;
            })
            .catch(function() {
                console.log("error");
            });

            // 7일간의 날씨 예보 가져오기
            fetch('https://api.openweathermap.org/data/2.5/onecall?lat=' + lat + '&lon=' + lon + '&exclude=minutely,alerts&appid=' + apiKey)
            .then(function(resp) { return resp.json() })
            .then(function(data) {
                var forecastDiv = document.getElementById('forecast');
                for (var i = 0; i < 7; i++) {
                    var day = data.daily[i];
                    forecastDiv.innerHTML += '<p>Day ' + (i + 1) + ': ' + day.weather[0].main + '</p>';
                }

                // 시간당 예보 그래프 그리기
                var ctx = document.getElementById('hourlyForecast').getContext('2d');
                new Chart(ctx, {
                    type: 'line',
                    data: {
                        labels: data.hourly.map((_, i) => i + '시간 후'),
                        datasets: [{
                            label: '온도 (°C)',
                            data: data.hourly.map(h => h.temp - 273.15),
                            borderColor: 'rgb(255, 99, 132)',
                            yAxisID: 'y',
                        }, {
                            label: '강수 확률 (%)',
                            data: data.hourly.map(h => h.pop * 100),
                            borderColor: 'rgb(54, 162, 235)',
                            yAxisID: 'y1',
                        }]
                    },
                    options: {
                        scales: {
                            y: {
                                beginAtZero: true
                            },
                            y1: {
                                beginAtZero: true
                            }
                        }
                    }
                });

                // 8일간 예보 그래프 그리기
                var ctx = document.getElementById('dailyForecast').getContext('2d');
                new Chart(ctx, {
                    type: 'line',
                    data: {
                        labels: data.daily.map((_, i) => i + '일 후'),
                        datasets: [{
                            label: '온도 (°C)',
                            data: data.daily.map(d => d.temp.day - 273.15),
                            borderColor: 'rgb(255, 99, 132)',
                            yAxisID: 'y',
                        }, {
                            label: '강수 확률 (%)',
                            data: data.daily.map(d => d.pop * 100),
                            borderColor: 'rgb(54, 162, 235)',
                            yAxisID: 'y1',
                        }]
                    },
                    options: {
                        scales: {
                            y: {
                                beginAtZero: true
                            },
                            y1: {
                                beginAtZero: true
                            }
                        }
                    }
                });
            })
            .catch(function() {
                console.log("error");
            });

            // 지도 표시하기
            var map = new ol.Map({
                target: 'map',
                layers: [
                    new ol.layer.Tile({
                        source: new ol.source.OSM()
                    })
                ],
                view: new ol.View({
                    center: ol.proj.fromLonLat([lon, lat]),
                    zoom: 10
                })
            });
        })
        .catch(function() {
            console.log("error");
        });
    </script>
</body>
</html>

This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.

More on the author's page

Keep reading

Renewal

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min