-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (84 loc) · 3.95 KB
/
script.js
File metadata and controls
136 lines (84 loc) · 3.95 KB
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
const searchBtn = document.getElementById("searchBtn");
const cityInput = document.querySelector(".cityInput");
const pastSearchesContainer = document.querySelector(".pastSearchContainer");
let pastSearchList = document.querySelector("#pastSearchList");
$(".pastSearchContainer").on('click', "button", function (event) {
event.preventDefault();
searchCity($(this).text());
})
let cities = JSON.parse(localStorage.getItem("cities"));
if (!cities) {
cities = [];
};
renderCities();
if (cities.length > 0) {
searchCity(cities[cities.length - 1]);
}
// create a search function to retrieve input from search bar on button click
function renderCities() {
$(".pastSearchContainer").empty();
for (let i = 0; i < cities.length; i++) {
let pastCitySearches = $("<button>").text(cities[i]);
$(".pastSearchContainer").append(pastCitySearches);
}
}
searchBtn.addEventListener('click', function (event) {
let cityName = $('.cityInput').val().trim();
cities.push(cityName);
renderCities();
localStorage.setItem("cities", JSON.stringify(cities));
if (cityName === "") {
return;
}
event.preventDefault();
searchCity(cityName);
})
function searchCity(cityName) {
const apiKey = "&appid=a6734ec95af468f519df63f9df4bfd44";
const unitsMetric = "&units=metric";
const queryURL = "https://api.openweathermap.org/data/2.5/weather?q=" + cityName + unitsMetric + apiKey;
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
let lat = ("lat=" + response.coord.lat);
let lon = ("&lon=" + response.coord.lon);
let date = ("&dt=" + response.dt);
//get legible date format, put in to a function??
const milliseconds = (response.dt) * 1000;
const dateObject = new Date(milliseconds);
const dateFormat = dateObject.toLocaleString()
$("#cityname").text(response.name + " " + dateFormat);
function searchWeather() {
const queryURL = "https://api.openweathermap.org/data/2.5/onecall?" + lat + lon + date + unitsMetric + "&exclude=minutely,hourly" + apiKey;
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
console.log(response);
$("#windSpeed").text("Wind speed: " + response.current.wind_speed + 'MPH');
$("#humidity").text("Humidity: " + response.current.humidity + "%");
$("#temperature").text("Temperature: " + response.current.temp + '°C');
$("#uvIndex").text("UV Index: " + response.current.uvi);
let iconcode = response.current.weather[0].icon;
let iconurl = "http://openweathermap.org/img/wn/" + iconcode + ".png";
$('#wicon').attr('src', iconurl);
// let iconCode = response.weather.icon;
// $("#icon").append('<img src= "http://openweathermap.org/img/wn/' + iconCode + '@2x.png">');
for (let i = 0; i < 5; i++) {
const forMilliseconds = (response.daily[i].dt) * 1000;
const forDateObject = new Date(forMilliseconds);
const forDateFormat = forDateObject.toLocaleString();
let forecasticoncode = (response.daily[i].weather[0].icon);
let forecasticonurl = "http://openweathermap.org/img/wn/" + forecasticoncode + ".png";
let currentCard = "#day" + (i + 1);
$(currentCard + " .forecastDate").text(forDateFormat);
$(currentCard + " .forecastHumidity").text("Humidity: " + response.daily[i].humidity + "%");
$(currentCard + " .forecastTemperature").text("Temperature: " + response.daily[i].temp.day + "°C");
$(currentCard + " .forecastIcon").attr('src', forecasticonurl);
}
})
}
searchWeather();
})
}