-
Notifications
You must be signed in to change notification settings - Fork 4
/
weather.js
62 lines (34 loc) · 2.19 KB
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
$(document).ready(function(){
$("#submitCity").click(function(){
return getWeather();
});
});
function getWeather(){
var city = $("#city").val();
if(city != ''){
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather?q=' + city + "&units=metric" + "&APPID=c10bb3bd22f90d636baa008b1529ee25",
type: "GET",
dataType: "jsonp",
success: function(data){
var widget = showResults(data)
$("#showWeather").html(widget);
$("#city").val('');
}
});
}else{
$("#error").html("<div class='alert alert-danger' id='errorCity'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>City field cannot be empty</div>");
}
}
function showResults(data){
return '<h2 style="font-weight:bold; font-size:30px; padding-top:20px;" class="text-center">Current Weather for '+data.name+', '+data.sys.country+'</h2>'+
"<h3 style='padding-left:40px;'><strong>Weather</strong>: "+data.weather[0].main+"</h3>"+
"<h3 style='padding-left:40px;'><strong>Description</strong>:<img src='http://openweathermap.org/img/w/"+data.weather[0].icon+".png'> "+data.weather[0].description+"</h3>"+
"<h3 style='padding-left:40px;'><strong>Temperature</strong>: "+data.main.temp+" °C</h3>"+
"<h3 style='padding-left:40px;'><strong>Pressure</strong>: "+data.main.pressure+" hpa</h3>"+
"<h3 style='padding-left:40px;'><strong>Humidity</strong>: "+data.main.humidity+"%</h3>"+
"<h3 style='padding-left:40px;'><strong>Min Temperature</strong>: "+data.main.temp_min+"°C</h3>"+
"<h3 style='padding-left:40px;'><strong>Max Temperature</strong>: "+data.main.temp_max+"°C</h3>"+
"<h3 style='padding-left:40px;'><strong>Wind Speed</strong>: "+data.wind.speed+"m/s</h3>"+
"<h3 style='padding-left:40px; padding-bottom:30px;'><strong>Wind Direction</strong>: "+data.wind.deg+"°</h3>";
}