-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
147 lines (130 loc) · 5.11 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
let now = new Date();
let time = document.querySelector("#time");
let min = now.getMinutes();
if (min < 10) {
min = "0" + min;
}
time.innerHTML = now.getHours() + ":" + min;
let days = [
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday",
];
let day = now.getDay();
let today = document.querySelector("#today");
today.innerHTML = days[day - 1];
let date = document.querySelector("#date");
date.innerHTML =
now.getMonth() + 1 + "/" + now.getDate() + "/" + now.getFullYear();
//-----------------------------------------------------------------
let description = document.querySelector("#weather");
let temperature = document.querySelector("#temperature");
let apiKey = "74e718c96dc64467c77303080c47c11d";
let humidity = document.querySelector("#humidity");
let windSpeed = document.querySelector("#wind");
function getCoordination(coordinates) {
let oneCallApi = `https://api.openweathermap.org/data/2.5/onecall?lat=${coordinates.lat}&lon=${coordinates.lon}&appid=5f472b7acba333cd8a035ea85a0d4d4c&units=metric`;
axios.get(oneCallApi).then(function (response) {
let forcastData = response.data.daily;
let forcastElement = document.querySelector(".next-days");
let forcast = "";
function correctDay(dt) {
let date = new Date(dt * 1000);
let day = date.getDay();
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return days[day];
}
forcastData.forEach(function (data, index) {
if (index < 5) {
forcast =
forcast +
`
<li>
${correctDay(data.dt)}
<img
class="forcast-image"
src="http://openweathermap.org/img/wn/${
data.weather[0].icon
}@2x.png"
alt=""
/>
<br />
<span class="max-forcast-temp">${Math.round(
data.temp.min
)}</span>/
<span class="min-forcast-temp">${Math.round(
data.temp.max
)}</span>
</li>
`;
forcastElement.innerHTML = forcast;
}
});
});
}
function showWeather(city) {
let Url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
axios.get(Url).then(function (response) {
celciusElement = Math.round(response.data.main.temp);
temperature.innerHTML = celciusElement;
description.innerHTML = response.data.weather[0].description;
humidity.innerHTML = response.data.main.humidity;
wind.innerHTML = Math.round(response.data.wind.speed);
if (response.data.weather[0].description.includes("cloud")) {
document.getElementById("image").src = "img/cloudy.svg";
} else if (response.data.weather[0].description.includes("rain")) {
document.getElementById("image").src = "img/rain.svg";
} else if (response.data.weather[0].description.includes("snow")) {
document.getElementById("image").src = "img/snow.svg";
} else {
document.getElementById("image").src = "img/sunny.svg";
}
getCoordination(response.data.coord);
});
}
function showCityName(e) {
e.preventDefault();
let input = document.querySelector("#search");
let cityName = document.querySelector(".city-name");
cityName.innerHTML = input.value;
showWeather(input.value);
}
let form = document.querySelector(".search-bar");
form.addEventListener("submit", showCityName);
//GeoLocation-button-------------------------------------------------------
function getLocation(position) {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
let UrlTwo = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=metric`;
axios.get(UrlTwo).then(function (response) {
description.innerHTML = response.data.weather[0].description;
temperature.innerHTML = Math.round(response.data.main.temp);
wind.innerHTML = Math.round(response.data.wind.speed);
humidity.innerHTML = response.data.main.humidity;
let cityName = document.querySelector(".city-name");
cityName.innerHTML = response.data.name;
if (response.data.weather[0].description.includes("cloud")) {
document.getElementById("image").src = "img/cloudy.svg";
} else if (response.data.weather[0].description.includes("rain")) {
document.getElementById("image").src = "img/rain.svg";
} else if (response.data.weather[0].description.includes("snow")) {
document.getElementById("image").src = "img/snow.svg";
} else if (response.data.weather[0].description.includes("mist")) {
document.getElementById("image").src = "img/mist.svg";
} else {
document.getElementById("image").src = "img/sunny.svg";
}
getCoordination(response.data.coord);
});
}
function displayCurrentTemp() {
navigator.geolocation.getCurrentPosition(getLocation);
}
let button = document.querySelector("#button");
button.addEventListener("click", displayCurrentTemp);
//---------------------------------------------------
//---------------------------------------------------------