-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiweather.js
181 lines (157 loc) · 5.81 KB
/
apiweather.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*Oconti01 - WDFMA 2018
Task2 - – Create a Weather Application based on API Data*/
$(document).ready(function() {
$("#country").change(function() {
var cityName;
var select = $("#country option:selected").val(); //get selected input value/switch case for countries list
switch (select) {
case "England":
cityName = "england";
city(cityName);
break;
case "Scotland":
cityName = "scotland";
city(cityName);
break;
case "Wales":
cityName = "wales";
city(cityName);
break;
case "Northern Ireland":
cityName = "nireland";
city(cityName);
break;
default:
$("#city").empty();
break;
}
});
$("#city").change(function() { //get city by name
var cityValue = $("#city").val();
getWeatherApi(cityValue);
});
function city(country) {
$("#city").empty(); // initialize empty
$("#city").append("<option>--Select--</option>");
$.get(country+"-cities.html", function( cities) { //get the html file containin cities
$("#city").append(cities);
});
}
function getWeatherApi(cityValue){ // get api data by cityname
var apiKey = '776c2da7715936b010d5b7394595eb34';
$.ajax( {
url:'http://api.openweathermap.org/data/2.5/weather?q='+cityValue+'&APPID='+apiKey ,
type: 'GET' ,
dataType: 'json' ,
success: function(response){
var sTxt = '' ;
var cond;
var temp ;
var windSpeed;
var windDeg;
var apiDate;
var name;
var icon;
$('#weatherInfo').html(' ');
$.each(response.weather, function(index, value) { // fetching required json data and exiting loop
cond = response.weather[index].main;
//tofix: chrome problem on loading weather icons - icons show up only on second load for each city
switch (cond) { //switch case for icons
case "Clouds":
icon = '<img src="./weather_icons/cloud.png" alt="cloud" width="100px" height="100%"/>';
break;
case "Hail":
icon = '<img src="./weather_icons/hail.png" alt="hail" width="100px" height="100%"/>';
break;
case "Heavy Cloud":
icon = '<img src="./weather_icons/heavy cloud.png" alt="heavy-clouds" width="100px" height="100%"/>';
break;
case "Heavy Rain":
icon = '<img src="./weather_icons/heavy rain.png" alt="heavy-rain" width="100px" height="100%"/>';
break;
case "Rain":
icon = '<img src="./weather_icons/rain.png" alt="rain" width="100px" height="100%"/>';
break;
case "Sleet":
icon = '<img src="./weather_icons/sleet.png" alt="sleet" width="100px" height="100%"/>';
break;
case "Snow":
icon = '<img src="./weather_icons/snow.png" alt="snow" width="100px" height="100%"/>';
break;
case "Sun":
icon = '<img src="./weather_icons/sun.png" alt="sun" width="100px" height="100%"/>';
break;
case "Sun and Clouds":
icon = '<img src="./weather_icons/sun and cloud.png" alt="sun-cloud" width="100px" height="100%"/>';
break
case "Thunderstorm":
icon = '<img src="./weather_icons/thunderstorm.png" alt="thunderstorm" width="100px" height="100%"/>';
break;
default: //fix for missing icons (clear, mist, drizzle icons missing)
icon = '';
break;
}
});
//assign api to local vars
$.each(response.main, function(index, value) {
temp = response.main.temp;
return false;
});
$.each(response.wind, function(index, value) {
windSpeed = response.wind.speed;
windDeg = response.wind.deg;
return false;
});
$.each(response, function(index) {
apiDate = response.dt ;
name = response.name;
return false;
});
sTxt += "<h2>"+name+", UK<br><span>"+unixToDate(apiDate)+"</span></h2>"+icon+"<p id=temp>"+ toCelsius(temp)+"℃<br>"+ cond+"</p><p id=right><img src=./weather_icons/wind.svg></img> "+ toMph(windSpeed)+"mph</p><p id=left><img src=./weather_icons/compass.svg></img>"+ degConv(windDeg)+"</p>";
$(' #weatherInfo').append(sTxt);
},
error: function() {
$(' #info').html(' <p>An error has occurred while retriving the data</p>');
}
});
//Conversions
function unixToDate(unix_timestamp){ // unix time to date conversion e.g. 14/02/2015
var date = new Date(unix_timestamp*1000);
var year = date.getFullYear();
var month = ("0"+(date.getMonth()+1)).substr(-2);
var day = ("0"+date.getDate()).substr(-2);
return day+"/"+month+"/"+year;
}
function toCelsius(kelvin){ // kelvin to celsius conversion
var celsius = kelvin - 273.15; // 0(kelvin) = -273.15 Celsius
var degree = Math.round(celsius)
return degree;
}
function toMph(knots){ // mph conversion
var mph = knots * 1.15078;
var speed = Math.round(mph);
return speed;
}
function degConv(deg){ //get wind direction as int and convert it to string
if (deg>0 && deg<25){
return "Northerly";
}else if (deg>25 && deg<65){
return "North easterly";
}else if (deg>65 && deg<115){
return "Easterly";
}else if (deg>115 && deg<155){
return "South easterly";
}else if (deg>155 && deg<205){
return "Southerly";
}else if (deg>205 && deg<245){
return "South westerly";
}else if (deg>245 && deg<295){
return "Westerly";
}else if (deg>295 && deg<335){
return "North westerly";
}else if (deg>335){
return "Northerly";
}
}
}
});