-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
139 lines (101 loc) · 5.43 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
// java-script file for the Weather Application
const cityInput = document.querySelector(".city-input");
const searchButton = document.querySelector(".search-btn");
const locationButton = document.querySelector(".location-btn");
const weatherCardsDiv = document.querySelector(".weather-cards");
const currentWeatherDiv = document.querySelector(".current-weather");
const API_KEY = "eb25bb98e70091ae7787a643b17b1686"; // API key for OpenWeatherMap API
const createWeatherCard = (cityName ,weatherItem ,index) =>{
if (index === 0) { //HTML for the main weather cards
return `<div class="details">
<h2>${cityName} (${weatherItem.dt_txt.split(" ")[0]})</h2>
<h4>Temperature : ${(weatherItem.main.temp - 273.15).toFixed(2)}°C</h4>
<h4>Wind Speed : ${weatherItem.wind.speed} M/s</h4>
<h4>Humidity : ${weatherItem.main.humidity} %</h4>
</div>
<div class="icon">
<img src="https://openweathermap.org/img/wn/${weatherItem.weather[0].icon}@2x.png" alt="weather-icon">
<h4>${weatherItem.weather[0].description}</h4>
</div>`;
} else {
return ` <li class="card">
<h3>${cityName} (${weatherItem.dt_txt.split(" ")[0]})</h3>
<img src="https://openweathermap.org/img/wn/${weatherItem.weather[0].icon}@2x.png" alt="weather-icon">
<h4>Desc : ${weatherItem.weather[0].description}</h4>
<h4>Temperature : ${(weatherItem.main.temp - 273.15).toFixed(2)}°C</h4>
<h4>Wind Speed : ${weatherItem.wind.speed} M/s</h4>
<h4>Humidity : ${weatherItem.main.humidity} %</h4>
</li>`;
}
}
const getWeatherDetails = (cityName ,lat ,lon) =>{
const WEATHER_API_URL = `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${API_KEY}`; // use 5 days forecast API of thet particular place.
fetch(WEATHER_API_URL).then(res => res.json()).then(data => {
// filter the Forecasts to get the only one forecast per day..
const uniqueForecasteDays=[];
const fiveDaysForecast = data.list.filter(forecast =>{
const forecastDate =new Date(forecast.dt_txt).getDate();
if(!uniqueForecasteDays.includes(forecastDate)){
return uniqueForecasteDays.push(forecastDate);
}
});
//clearing previous weather data
cityInput.value = "";
currentWeatherDiv.innerHTML= "";
weatherCardsDiv.innerHTML= "";
//console.log(fiveDaysForecast);
// creating weather cards and adding them to the DOM
fiveDaysForecast.forEach((weatherItem ,index) => {
if(index === 0){
currentWeatherDiv.insertAdjacentHTML("beforeend" ,createWeatherCard(cityName ,weatherItem , index));
}else{
weatherCardsDiv.insertAdjacentHTML("beforeend" ,createWeatherCard(cityName ,weatherItem , index));
}
});
}).catch(() => {
alert("An Error Occured while Fetching the Weather Forecast !")
})
}
const getCityCoordinates = () =>{
const cityName = cityInput.value.trim(); //get the users entered city name
if (!cityName) return ; // return if cityName is empty.
// console.log(cityName);
const GEOCODING_API_URL = `https://api.openweathermap.org/geo/1.0/direct?q=${cityName}&limit=1&appid=${API_KEY}` ; // it provide the location lattitude & longitude -- by using the ---> OpenWeatherMap API
//get entered city coordinates (lattitude ,longitude and name ) from the API response .
fetch(GEOCODING_API_URL).then(res => res.json()).then(data => {
// console.log(data)
if(!data.length) return alert(`No Coordinates found for ${cityName}`);
const { name ,lat ,lon } = data[0];
getWeatherDetails(name,lat ,lon);
}).catch(() => {
alert("An Error Occured while Fetching the Coordinates!")
});
}
const getUserCoordinates = ()=>{
navigator.geolocation.getCurrentPosition(
position =>{
//console.log(position);
const { latitude, longitude} = position.coords;
//Get city name for the coordinates using reverse geocoding API .
const REVERSE_GEOCODING_URL = `http://api.openweathermap.org/geo/1.0/reverse?lat=${latitude}&lon=${longitude}&limit=1&appid=${API_KEY}` ;
fetch(REVERSE_GEOCODING_URL).then(res => res.json()).then(data => {
// console.log(data)
// if(!data.length) return alert(`No City found for ${cityName}`);
const { name } = data[0];
getWeatherDetails(name,latitude ,longitude);
// console.log(data);
}).catch(() => {
alert("An Error Occured while Fetching the City!")
});
},
error =>{
//console.log(error);
if (error.code === error.PERMISSION_DENIED) {
alert("Geolocation request denied . Please reset loacation permission to grant access again...")
}
}
);
}
searchButton.addEventListener("click", getCityCoordinates);
locationButton.addEventListener("click", getUserCoordinates);
cityInput.addEventListener("keyup", e => e.key === "Enter" &&getCityCoordinates());