-
Notifications
You must be signed in to change notification settings - Fork 0
/
getWeather.js
68 lines (51 loc) · 2.51 KB
/
getWeather.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
63
64
65
66
67
68
function getWeather() {
$('#cityNameDisplay').html('');
$('#weatherConditions').html('');
$('.temperature').html('');
$('#humidity').html('');
var cityName = $('#cityName').val();
var openweatherApiCall = 'http://api.openweathermap.org/data/2.5/weather?q='+cityName+'&appid=1b1e4ef557bca085d894a76b85555c2f';
$.getJSON(openweatherApiCall,weatherCallBack);
function weatherCallBack(weatherData) {
var cityName = weatherData.name;
var country = weatherData.sys.country;
var id = weatherData.weather[0].id;
var description = weatherData.weather[0].description;
var temperature = weatherData.main.temp-273.15;
temperature = Math.round((temperature + Number.EPSILON) );
var humidity = weatherData.main.humidity;
var icon = weatherData.weather[0].icon;
var bgColour = '';
var img = 'http://openweathermap.org/img/wn/'+icon+'@2x.png';
function changeColour(description) {
if(description == 'clear sky') {
bgColour = '#50e6ff';
} else if (description == 'few clouds') {
bgColour = '#50e6ff';
} else if (description == 'scattered clouds') {
bgColour = '#50e6ff';
} else if (description == 'broken clouds') {
bgColour = '#dfdfdf ';
} else if (description == 'shower rain') {
bgColour = '#0078d4';
} else if (description == 'rain') {
bgColour = '#0078d4';
} else if (description == 'thunderstorm') {
bgColour = '#006ca4';
} else if (description == 'snow') {
bgColour = '#dfdfdf';
} else if (description == 'mist') {
bgColour = '#a8a8a8';
}
$('html').css('background-color',bgColour);
$('.jumbotron').css('background-color',bgColour);
var image = document.getElementById('weatherImage');
image.src = img;
}
$('#cityNameDisplay').append(cityName+", "+country);
$('#weatherConditions').append(description);
$('.temperature').append(temperature+"℃");
$('.humidity').append("Humidity: "+humidity+"%");
changeColour(description);
}
}