duy khanh duy khanh - 4 months ago 11
jQuery Question

How to know when the weather is rain or snow or sun?

first i'm sorry because my Eng too bad. I have a question, how i know is it rain or snow ? i try check by temp but its not right . I take data form openweathermap API .
here is my codepen:
http://codepen.io/khanh1993/full/EybPXG/
and condition to check:

if(fTemp>80){
$("#icon").html('<i class="fa fa-sun-o fa-fw fa-5x fa-spin" aria-hidden="true"></i>');
$('body').css("background","url('http://www.walldevil.com/wallpapers/a79/nature-backgrounds-screen-desktop-savers-high-sunshine-wallpapers-images-resolution.jpg')")
}
else if(fTemp>58){
$("#icon").html('<i class="fa fa-tint fa-fw fa-5x" aria-hidden="true"></i>');
$('body').css("background","url('http://wallpapercave.com/wp/doNuhh5.jpg')");
}
else if(fTemp>40){
$('#icon').html('<i class="fa fa-cloud fa-5x fa-fw" aria-hidden="true"></i>');
$('body').css("background","url('http://eskipaper.com/images/cloud-background-6.jpg')");
}

Answer

This is the API doc that i found in openweather API

{"coord":{"lon":139,"lat":35},
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
"wind":{"speed":7.31,"deg":187.002},
"rain":{"3h":0},
"clouds":{"all":92},
"dt":1369824698,
"id":1851632,
"name":"Shuzenji",
"cod":200}

you can get the weather from response.weather[0].main

here is how i get that

$.getJSON('http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=356c0e149219f8d18d30e05f4e1a130f', function(response){
    console.log(response.weather[0].main)
});

the output will be "Rain" right now

Comments