-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
155 lines (139 loc) · 4.16 KB
/
app.js
File metadata and controls
155 lines (139 loc) · 4.16 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
const key = "a9f97e4af8263f6c0367aa8155832086";
const timeEl = document.getElementById("time");
const dateEl = document.getElementById("date");
const currentWeather = document.getElementById("current-weather-items");
const timezone = document.getElementById("time-zone");
const countryEl = document.getElementById("country");
const weatherForecastEl = document.getElementById("weather-forecast");
let city;
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"ThursDay",
"Friday",
"Saturday",
];
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
setInterval(() => {
const time = new Date();
const month = time.getMonth();
const date = time.getDate();
const day = time.getDay();
var hour = time.getHours();
var ampm;
if (hour >= 12) {
ampm = "PM";
} else {
ampm = "AM";
}
if (hour >= 13) {
hour = hour % 12;
}
const minutes = time.getMinutes();
timeEl.innerHTML =
(hour < 10 ? "0" + hour : hour) +
":" +
(minutes < 10 ? "0" + minutes : minutes) +
`<span id="am-pm">${ampm}</span>`;
dateEl.innerHTML = `${days[day]}, ${date} ${months[month]}`;
}, 1000);
const liveData = () => {
navigator.geolocation.getCurrentPosition((success) => {
let lat = success.coords.latitude;
let lon = success.coords.longitude;
fetch(
`https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=hourly,minutely&appid=${key}&units=metric`
)
.then((res) => res.json())
.then((data) => {
showWeatherData(data);
});
});
};
liveData();
async function findCoord() {
try {
event.preventDefault();
let city = document.getElementById("searchData").value;
let res = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${key}&units=metric`
);
let data = await res.json();
let d = data.coord;
return d;
} catch (error) {
console.log("error:", error);
}
}
const getData = async () => {
try {
let coord = await findCoord();
const lati = coord.lat;
const long = coord.lon;
let res = await fetch(
`https://api.openweathermap.org/data/2.5/onecall?lat=${lati}&lon=${long}&exclude=hourly,minutely&appid=${key}&units=metric`
);
let data = await res.json();
showWeatherData(data);
} catch (error) {
console.log("error:", error);
}
};
let btn = document.getElementById("search");
btn.addEventListener("click", getData);
function showWeatherData(data) {
let { humidity, pressure, sunrise, sunset, wind_speed } = data.current;
timezone.innerHTML = `TimeZone: ${data.timezone}`;
countryEl.innerHTML = `${data.lat}N ${data.lon}S`;
document.getElementById(
"iframe"
).src = `https://maps.google.com/maps?q=${data.lat},${data.lon}&t=k&z=13&ie=UTF8&iwloc=&output=embed`;
currentWeather.innerHTML = `<div class="weather-item">
<div>Humidity</div>
<div>${humidity} %</div>
</div>
<div class="weather-item">
<div>Pressure</div>
<div>${pressure} hPa</div>
</div>
<div class="weather-item">
<div>Wind Speed</div>
<div>${wind_speed} m/s N</div>
</div>
<div class="weather-item">
<div>Sunrise</div>
<div>${window.moment(sunrise * 1000).format("HH:mm a")}</div>
</div>
<div class="weather-item">
<div>Sunset</div>
<div>${window.moment(sunset * 1000).format("HH:mm a")}</div>
</div>`;
let otherDayForecast = "";
daily = data.daily;
daily.forEach((day, i) => {
otherDayForecast += `<div class="weather-forecast-item">
<div class="day">${window.moment(day.dt * 1000).format("ddd")}</div>
<img src='http://openweathermap.org/img/wn/${
day.weather[0].icon
}@2x.png' alt="weather icon" class="w-icon" />
<div class="temp">Day - ${day.temp.day}°C</div>
<div class="temp">Night - ${day.temp.night}°C</div>
</div>`;
weatherForecastEl.innerHTML = otherDayForecast;
});
}