-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
367 lines (281 loc) · 13.3 KB
/
script.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
$(document).ready(function () {
//function for button click
$("#citySearch").on("click", function (event) {
event.preventDefault();
displayInputCurrentWeather();
displayInput5DayForecast();
var value = $(this).siblings("#city").val().toUpperCase();
var city = $(this).parent().attr("id");
localStorage.setItem(city, value);
showRecent();
displayHistory();
});
// local storage
var searchedCity = localStorage.getItem("searchTerm");
$(".recentCities").text(searchedCity);
//display weather via recent search
function displayCurrentWeather() {
var cityName = "Taylor";
if (localStorage.getItem("searchTerm") !== null) {
cityName = localStorage.getItem("searchTerm")
}
var queryURL = "https://api.openweathermap.org/data/2.5/weather?q=" + cityName + "&appid=63d971fb63a8453453f24fc684d7d368&units=imperial";
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
$("#currentWeather").empty();
var currentWeather = $("#currentWeather").append($("<div class='currentWeather'>"));
currentWeather.append($("<h2>")).text("Current Weather");
var icon = response.weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
currentWeather.append(weatherImg);
var city = response.name;
var pOne = $("<p>").text("City: " + city);
currentWeather.append(pOne);
var date = moment.unix(response.dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
currentWeather.append(pTwo);
var temperature = response.main.temp;
var pThree = $("<p>").text("Temperature: " + temperature + " \u2109");
currentWeather.append(pThree);
var humidity = response.main.humidity;
var pFour = $("<p>").text("Humidity: " + humidity + "%");
currentWeather.append(pFour);
var windSpeed = response.wind.speed;
var pFive = $("<p>").text("Wind Speed: " + windSpeed + " mph");
currentWeather.append(pFive);
var lat = response.coord.lat;
var lon = response.coord.lon;
var cityNameUV = $("#city").val().trim();
if (localStorage.getItem("searchTerm") !== null) {
cityNameUV = localStorage.getItem("searchTerm")
}
var queryURLUV = "https://api.openweathermap.org/data/2.5/uvi?lat=" + lat + "&lon=" + lon + "&appid=63d971fb63a8453453f24fc684d7d368&units=imperial";
$.ajax({
url: queryURLUV,
method: "GET"
}).then(function (response) {
var uvIndex = response.value;
var pSix = $("<p>").text("UV Index: " + uvIndex);
currentWeather.append(pSix);
});
});
};
//display current weather for default city on page load
displayCurrentWeather();
//displays current weather for user-input city
function displayInputCurrentWeather() {
var cityName = $("#city").val().trim();
if (localStorage.getItem("searchTerm") !== null) {
cityNameUV = localStorage.getItem("searchTerm")
}
var queryURL = "https://api.openweathermap.org/data/2.5/weather?q=" + cityName + "&appid=63d971fb63a8453453f24fc684d7d368&units=imperial";
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
$("#currentWeather").empty();
var currentWeather = $("#currentWeather").append($("<div class='currentWeather'>"));
currentWeather.append($("<h2>")).text("Current Weather");
var icon = response.weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
currentWeather.append(weatherImg);
var city = response.name;
var pOne = $("<p>").text("City: " + city);
currentWeather.append(pOne);
var date = moment.unix(response.dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
currentWeather.append(pTwo);
var temperature = response.main.temp;
var pThree = $("<p>").text("Temperature: " + temperature + " \u2109");
currentWeather.append(pThree);
var humidity = response.main.humidity;
var pFour = $("<p>").text("Humidity: " + humidity + "%");
currentWeather.append(pFour);
var windSpeed = response.wind.speed;
var pFive = $("<p>").text("Wind Speed: " + windSpeed + " mph");
currentWeather.append(pFive);
var lat = response.coord.lat;
var lon = response.coord.lon;
var cityNameUV = $("#city").val().trim();
if (localStorage.getItem("searchTerm") !== null) {
cityNameUV = localStorage.getItem("searchTerm")
}
var queryURLUV = "https://api.openweathermap.org/data/2.5/uvi?lat=" + lat + "&lon=" + lon + "&appid=63d971fb63a8453453f24fc684d7d368";
$.ajax({
url: queryURLUV,
method: "GET"
}).then(function (response) {
var uvIndex = response.value;
var pSix = $("<p>").text("UV Index: " + uvIndex);
currentWeather.append(pSix);
});
});
};
//displays 5-day forecast for default city
function display5DayForecast() {
var cityName = "Taylor";
if (localStorage.getItem("searchTerm") !== null) {
cityName = localStorage.getItem("searchTerm")
}
var queryURL = "https://api.openweathermap.org/data/2.5/forecast?q=" + cityName + "&mode=json&units=imperial&appid=63d971fb63a8453453f24fc684d7d368";
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
var date = moment.unix(response.list[0].dt).format("MM/DD/YYYY");
var pOne = $("<p>").text("Date: " + date);
$("#dayOne").append(pOne);
var icon = response.list[3].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
$("#dayOne").append(weatherImg);
var temperature = response.list[3].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
$("#dayOne").append(pThree);
var humidity = response.list[3].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
$("#dayOne").append(pFour);
date = moment.unix(response.list[8].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
$("#dayTwo").append(pTwo);
var icon = response.list[11].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
$("#dayTwo").append(weatherImg);
var temperature = response.list[11].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
$("#dayTwo").append(pThree);
var humidity = response.list[11].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
$("#dayTwo").append(pFour);
date = moment.unix(response.list[16].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
$("#dayThree").append(pTwo);
var icon = response.list[19].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
$("#dayThree").append(weatherImg);
var temperature = response.list[19].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
$("#dayThree").append(pThree);
var humidity = response.list[19].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
$("#dayThree").append(pFour);
date = moment.unix(response.list[24].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
$("#dayFour").append(pTwo);
var icon = response.list[27].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
$("#dayFour").append(weatherImg);
var temperature = response.list[27].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
$("#dayFour").append(pThree);
var humidity = response.list[27].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
$("#dayFour").append(pFour);
date = moment.unix(response.list[32].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
$("#dayFive").append(pTwo);
var icon = response.list[35].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
$("#dayFive").append(weatherImg);
var temperature = response.list[35].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
$("#dayFive").append(pThree);
var humidity = response.list[35].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
$("#dayFive").append(pFour);
});
};
//display 5-day forecast for default city on page load
display5DayForecast();
//display 5-day forecast for user-input city
function displayInput5DayForecast() {
var cityName = $("#city").val().trim();
var queryURL = "https://api.openweathermap.org/data/2.5/forecast?q=" + cityName + "&mode=json&units=imperial&appid=63d971fb63a8453453f24fc684d7d368";
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
$("#forecastWeather").empty();
var dayOneForecast = $("#forecastWeather").append($("<div class='dayOneWeather'>"));
var date = moment.unix(response.list[0].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
dayOneForecast.append(pTwo);
var icon = response.list[3].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
dayOneForecast.append(weatherImg);
var temperature = response.list[3].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
dayOneForecast.append(pThree);
var humidity = response.list[3].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
dayOneForecast.append(pFour);
var dayTwoForecast = $("#forecastWeather").append($("<div class='dayTwoWeather'>"));
date = moment.unix(response.list[8].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
dayTwoForecast.append(pTwo);
var icon = response.list[11].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
dayTwoForecast.append(weatherImg);
var temperature = response.list[11].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
dayTwoForecast.append(pThree);
var humidity = response.list[11].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
dayTwoForecast.append(pFour);
var dayThreeForecast = $("#forecastWeather").append($("<div class='dayThreeWeather'>"));
date = moment.unix(response.list[16].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
dayThreeForecast.append(pTwo);
var icon = response.list[19].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
dayThreeForecast.append(weatherImg);
var temperature = response.list[19].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
dayThreeForecast.append(pThree);
var humidity = response.list[19].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
dayThreeForecast.append(pFour);
var dayFourForecast = $("#forecastWeather").append($("<div class='dayFourWeather'>"));
date = moment.unix(response.list[24].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
dayFourForecast.append(pTwo);
var icon = response.list[27].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
dayFourForecast.append(weatherImg);
var temperature = response.list[27].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
dayFourForecast.append(pThree);
var humidity = response.list[27].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
dayFourForecast.append(pFour);
var dayFiveForecast = $("#forecastWeather").append($("<div class='dayFiveWeather'>"));
date = moment.unix(response.list[32].dt).format("MM/DD/YYYY");
var pTwo = $("<p>").text("Date: " + date);
dayFiveForecast.append(pTwo);
var icon = response.list[35].weather[0].icon;
var weatherImg = $("<img>").attr("src", ("https://openweathermap.org/img/w/" + icon + ".png"));
dayFiveForecast.append(weatherImg);
var temperature = response.list[35].main.temp;
var pThree = $("<p>").text("Temp. " + temperature + " \u2109");
dayFiveForecast.append(pThree);
var humidity = response.list[35].main.humidity;
var pFour = $("<p>").text("Humidity " + humidity + "%");
dayFiveForecast.append(pFour);
});
};
function showRecent() {
if (localStorage.getItem("searchTerm")) {
$(".recentCities").css("display", "block");
$(".recentSearches").css("display", "block");
}
}
showRecent();
// recently searched cities
function displayHistory() {
$(".recentCities").prepend($("<div class='searchAgain'>")).prepend($("#city").val().toUpperCase().trim());
};
// previously searched city
$(".searchAgain").on("click", function () {
});
});