210425 JavaScript jQuery Widget 풀이 (weather.js)

ITisIT210·2021년 4월 27일
0

jQuery

목록 보기
78/142
post-thumbnail
$(document).ready(function() {
    var apiKey = "본인의 apiKey 입력"

    var success = function(pos) {
        var lat = pos.coords.latitude;
        var lon = pos.coords.longitude;

        var apiCall = `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=${apiKey}`
        fetch(apiCall)
        .then(function(_data){
            return _data.json();
        })
        .then(function(weather){
            console.log(weather)
            var city = weather.name;
            var temp = weather.main.temp;
            var desc = weather.weather[0].icon;
            
            $("span.ico").css("backgroundImage",`url(http://openweathermap.org/img/wn/${desc}@2x.png)`)
            $("span.temp").text(temp);
            $("span.city").text(city);
        })
    }

    var err = function(err){
        console.log(err);
    }
    navigator.geolocation.getCurrentPosition(success,err);
})
profile
Engineering is the closest thing to magic that exists in the world.

0개의 댓글

관련 채용 정보