-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
161 lines (139 loc) · 5.63 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
let today = moment().format("dddd, MMMM D, YYYY");
let tomorrow = moment(new Date())
.add(1, "days")
.format("dddd");
let dayAfterTomorrow = moment(new Date())
.add(2, "days")
.format("dddd");
let twoDaysAfterTomorrow = moment(new Date())
.add(3, "days")
.format("dddd");
function handleSubmit(event) {
event.preventDefault();
let keyword = document.querySelector("#keyword");
search(keyword.value);
}
function showCurrentWeather(response) {
let currentTemperature = document.querySelector("#currentTemperature");
currentTemperature.innerHTML = `${Math.round(response.data.main.temp)}°C`;
let place = document.querySelector("#place");
place.innerHTML = response.data.name;
let weatherDescription = document.querySelector("#weatherDescription");
weatherDescription.innerHTML = response.data.weather[0].description;
let humidity = document.querySelector("#humidity");
humidity.innerHTML = `Humidity: ${response.data.main.humidity} %`;
let wind = document.querySelector("#wind");
wind.innerHTML = `Wind: ${Math.round(response.data.wind.speed)} m/s`;
let displayCurrentDate = document.querySelector("#currentDate");
displayCurrentDate.innerHTML = `${today}`;
let iconCurrentWeather = document.querySelector("#iconCurrentWeather");
iconCurrentWeather.setAttribute(
"src",
"http://openweathermap.org/img/w/" + response.data.weather[0].icon + ".png"
);
const types = [
"Clear",
"Drizzle",
"Rain",
"Clouds",
"Atmosphere",
"Thunderstorm"
];
types.forEach(type => {
const musicType = `music${type}`;
if (response.data.weather[0].main === type) {
document.getElementById(musicType).style.display = "block";
} else {
document.getElementById(musicType).style.display = "none";
}
});
}
function showForecast(response) {
console.log(response.data);
let displayTomorrow = document.querySelector("#tomorrow");
displayTomorrow.innerHTML = `${tomorrow}`;
let iconTomorrowWeather = document.querySelector("#iconTomorrowWeather");
iconTomorrowWeather.setAttribute(
"src",
"http://openweathermap.org/img/w/" +
response.data.list[7].weather[0].icon +
".png"
);
let tomorrowTemperature = document.querySelector("#tomorrowTemperature");
tomorrowTemperature.innerHTML = `${Math.round(
response.data.list[7].main.temp
)}°C`;
let displayDayAfterTomorrow = document.querySelector("#dayAfterTomorrow");
displayDayAfterTomorrow.innerHTML = `${dayAfterTomorrow}`;
let iconDayAfterTomorrowWeather = document.querySelector(
"#iconDayAfterTomorrowWeather"
);
iconDayAfterTomorrowWeather.setAttribute(
"src",
"http://openweathermap.org/img/w/" +
response.data.list[15].weather[0].icon +
".png"
);
let dayAfterTomorrowTemperature = document.querySelector(
"#dayAfterTomorrowTemperature"
);
dayAfterTomorrowTemperature.innerHTML = `${Math.round(
response.data.list[15].main.temp
)}°C`;
let displayTwoDaysAfterTomorrow = document.querySelector(
"#twoDaysAfterTomorrow"
);
displayTwoDaysAfterTomorrow.innerHTML = `${twoDaysAfterTomorrow}`;
let iconTwoDaysAfterTomorrowWeather = document.querySelector(
"#iconTwoDaysAfterTomorrowWeather"
);
iconTwoDaysAfterTomorrowWeather.setAttribute(
"src",
"http://openweathermap.org/img/w/" +
response.data.list[23].weather[0].icon +
".png"
);
let twoDaysAfterTomorrowTemperature = document.querySelector(
"#twoDaysAfterTomorrowTemperature"
);
twoDaysAfterTomorrowTemperature.innerHTML = `${Math.round(
response.data.list[23].main.temp
)}°C`;
}
function search(city) {
let apiKey = "9b41700e7dcfb48ddf3be545946f91cc";
let urlCurrentCity = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
axios.get(urlCurrentCity).then(showCurrentWeather);
let urlForecast = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}&units=metric`;
axios.get(urlForecast).then(showForecast);
}
let form = document.querySelector("form");
form.addEventListener("submit", handleSubmit);
function displayCurrentWeatherCurrentLocation() {
function showWeatherCurrentLocation(response) {
let place = document.querySelector("#place");
place.innerHTML = response.data.name;
let currentTemperature = document.querySelector("#currentTemperature");
currentTemperature.innerHTML = `${Math.round(response.data.main.temp)}°C`;
let weatherDescription = document.querySelector("#weatherDescription");
weatherDescription.innerHTML = response.data.weather[0].description;
let humidity = document.querySelector("#humidity");
humidity.innerHTML = `Humidity: ${response.data.main.humidity} %`;
let wind = document.querySelector("#wind");
wind.innerHTML = `Wind: ${Math.round(response.data.wind.speed)} m/s`;
let displayCurrentDate = document.querySelector("#currentDate");
displayCurrentDate.innerHTML = `${today}`;
}
function newPosition(position) {
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
let apiKey = "9b41700e7dcfb48ddf3be545946f91cc";
let urlCurrentGPS = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=metric`;
axios.get(urlCurrentGPS).then(showCurrentWeather);
let urlForecastGPS = `https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=metric`;
axios.get(urlForecastGPS).then(showForecast);
}
navigator.geolocation.getCurrentPosition(newPosition);
}
let currentLocation = document.querySelector("#currentLocation");
currentLocation.addEventListener("click", displayCurrentWeatherCurrentLocation);