forked from jc721/weatherapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·190 lines (174 loc) · 5.6 KB
/
app.js
File metadata and controls
executable file
·190 lines (174 loc) · 5.6 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
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
const cities = [];
cities.push({
name: "Barcelona",
latitude: 41.41,
longitude: 2.19,
});
cities.push({
name: "Tokyo",
latitude: 35.65,
longitude: 139.74,
});
cities.push({
name: "Tallinn",
latitude: 59.43,
longitude: 24.75,
});
cities.push({
name: "London",
latitude: 51.5,
longitude: 0.12,
});
const apikey = "a75595ec0a1e42379ec6d18c703db66a";
let latitude;
let longitude;
const notification = document.getElementsByClassName("notification")[0];
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(render, onError);
} else {
notification.innerHTML = "Geolocation is not supported by this browser.";
}
}
function render(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
const get = fetch(
"https://api.weatherbit.io/v2.0/current?" +
"lat=" +
latitude +
"&lon=" +
longitude +
"&key=" +
apikey
);
get
.then((response) => response.json())
.then((get) => {
const location = get.data[0].city_name;
const loacationHtmlElement = document.getElementsByClassName(
"location"
)[0];
loacationHtmlElement.innerHTML = "<p>" + location + "</p>";
const temp = get.data[0].temp.toFixed(1);
const tempHtmlElement = document.getElementsByClassName(
"temperature-value"
)[0];
tempHtmlElement.innerHTML = "<p>" + temp + "°<span>C</span></p>";
const icon = get.data[0].weather.icon;
const iconHtmlElement = document.getElementsByClassName(
"weather-icon"
)[0];
iconHtmlElement.innerHTML =
"<img src='icons/" + icon + ".png' alt=''></img>";
const weatherDescription = get.data[0].weather.description;
const weatherDescriptionHtmlElement = document.getElementsByClassName(
"temperature-description"
)[0];
weatherDescriptionHtmlElement.innerHTML =
"<p>" + weatherDescription + "</p>";
})
.catch((error) => {
console.error("Error:", error);
});
}
function onError(error) {
console.error(error);
const p = document.createElement("p");
p.innerHTML = error.message;
notification.style.display = "block";
notification.appendChild(p);
}
getLocation();
function getWeather() {
let userCity = document.getElementById("userCity").value;
const getCity = fetch(
"https://api.weatherbit.io/v2.0/current?" +
"city=" +
userCity +
"&key=" +
apikey
);
getCity
.then((response) => response.json())
.then((get) => {
const location = get.data[0].city_name;
const loacationHtmlElement = document.getElementsByClassName(
"location"
)[0];
loacationHtmlElement.innerHTML = "<p>" + location + "</p>";
const temp = get.data[0].temp.toFixed(1);
const tempHtmlElement = document.getElementsByClassName(
"temperature-value"
)[0];
tempHtmlElement.innerHTML = "<p>" + temp + "°<span>C</span></p>";
const icon = get.data[0].weather.icon;
const iconHtmlElement = document.getElementsByClassName(
"weather-icon"
)[0];
iconHtmlElement.innerHTML =
"<img src='icons/" + icon + ".png' alt=''></img>";
const weatherDescription = get.data[0].weather.description;
const weatherDescriptionHtmlElement = document.getElementsByClassName(
"temperature-description"
)[0];
weatherDescriptionHtmlElement.innerHTML =
"<p>" + weatherDescription + "</p>";
})
.catch((error) => {
console.error("Error:", error);
});
}
const favCities = (latitude, longitude) => {
fetch(
`https://api.weatherbit.io/v2.0/current?lat=${latitude}&lon=${longitude}&key=${apikey}`
)
.then((response) => response.json())
.then((weather) => responseBody(weather));
};
for (let i = 0; i < cities.length; i++) {
favCities(cities[i].latitude, cities[i].longitude);
}
const responseBody = (get) => {
let favCity = document.getElementById("favCities");
let container = document.createElement("div");
container.className = "cityContainer";
favCity.appendChild(container);
let title = document.createElement("div");
title.className = "city-title";
let p = document.createElement("p");
p.innerHTML = get.data[0].city_name;
title.appendChild(p);
container.appendChild(title);
let notif = document.createElement("div");
container.appendChild(notif);
let weatherContainer = document.createElement("div");
weatherContainer.className = "city-container";
container.appendChild(weatherContainer);
let weatherIcon = document.createElement("div");
weatherIcon.className = "city-icon";
let icon = document.createElement("img");
icon.src = "icons/" + get.data[0].weather.icon + ".png";
weatherIcon.appendChild(icon);
weatherContainer.appendChild(weatherIcon);
let temp = document.createElement("div");
temp.className = "temperature-city";
let par = document.createElement("p");
par.innerHTML = get.data[0].temp.toFixed(1);
temp.appendChild(par);
weatherContainer.appendChild(temp);
let description = document.createElement("div");
description.className = "temperature-description";
let descPar = document.createElement("p");
descPar.innerHTML = get.data[0].weather.description;
description.appendChild(descPar);
weatherContainer.appendChild(description);
let location = document.createElement("div");
location.className = "location";
let locPar = document.createElement("p");
locPar.innerHTML = get.data[0].city_name;
location.appendChild(locPar);
weatherContainer.appendChild(location);
};
let button = document.getElementById("get");
button.addEventListener("click", getWeather);