-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
220 lines (184 loc) · 9.16 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
document.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
const searchButton = document.querySelector('.search-button');
if (searchButton) {
searchButton.click();
}
}
});
const
userLocation = document.getElementById("userLocation"),
converter = document.getElementById("converter"),
weatherIcon = document.querySelector(".weatherIcon"),
temperature = document.querySelector(".temperature"),
feelsLike = document.querySelector(".feelsLike"),
description = document.querySelector(".description"),
date = document.querySelector(".date"),
city = document.querySelector(".city"),
HValue = document.getElementById("HValue"),
WSValue = document.getElementById("WSValue"),
SRValue = document.getElementById("SRValue"),
SSValue = document.getElementById("SSValue"),
CValue = document.getElementById("CValue"),
UIValue = document.getElementById("UIValue"),
PValue = document.getElementById("PValue"),
Forecast = document.querySelector(".Forecast");
WEATHER_API_ENDPOINT= `https://api.openweathermap.org/data/2.5/weather?appid=a5bb4718b30b6f58f58697997567fffa&q=`;
WEATHER_DATA_ENDPOINT=`https://api.openweathermap.org/data/2.5/onecall?appid=a5bb4718b30b6f58f58697997567fffa&exclude=minutely&units=metric&`;
const WEATHER_API_KEY = "a5bb4718b30b6f58f58697997567fffa";
const WEATHER_API_URL = "https://api.openweathermap.org/data/2.5/weather";
const GEO_API_URL = "https://api.openweathermap.org/geo/1.0/direct";
// Run when the document is fully loaded
document.addEventListener('DOMContentLoaded', () => {
// Automatically request location and fetch weather data
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(fetchWeatherFromCoords, showError);
}
const userLocationInput = document.getElementById("userLocation");
userLocationInput.addEventListener("input", handleAutocomplete);
// Close autocomplete dropdown when clicking outside
document.addEventListener("click", function(event) {
if (!event.target.matches(".autocomplete-item, #userLocation")) {
const autocompleteDropdown = document.getElementById("autocomplete-dropdown");
autocompleteDropdown.style.display = "none";
}
});
});
// Fetch weather based on user's current coordinates
function fetchWeatherFromCoords(position) {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
const weatherAPI = `${WEATHER_API_URL}?lat=${lat}&lon=${lon}&appid=${WEATHER_API_KEY}&units=metric`;
fetch(weatherAPI)
.then(response => response.json())
.then(data => {
document.getElementById("userLocation").value = data.name;
finduserLocation(); // Use existing function to display weather
})
.catch(error => console.error("Error fetching weather data:", error));
}
// Show error if geolocation fails
function showError(error) {
console.error(`Geolocation Error: ${error.message}`);
}
// Handle autocomplete search as the user types
function handleAutocomplete() {
const query = document.getElementById("userLocation").value.trim();
const dropdown = document.getElementById("autocomplete-dropdown");
if (query.length < 3) {
dropdown.style.display = "none";
return;
}
const autocompleteAPI = `${GEO_API_URL}?q=${query}&limit=10&appid=${WEATHER_API_KEY}`;
fetch(autocompleteAPI)
.then(response => response.json())
.then(data => {
if (data.length > 0) {
dropdown.innerHTML = "";
// Separate Indian cities and other cities
const indianCities = data.filter(place => place.country === "IN");
const otherCities = data.filter(place => place.country !== "IN");
// Sort function to prioritize exact matches and then by population
const sortCities = (a, b) => {
if (a.name.toLowerCase() === query.toLowerCase()) return -1;
if (b.name.toLowerCase() === query.toLowerCase()) return 1;
return b.population - a.population;
};
// Sort both arrays
indianCities.sort(sortCities);
otherCities.sort(sortCities);
// Combine arrays, with Indian cities first
const sortedCities = [...indianCities, ...otherCities].slice(0, 5);
sortedCities.forEach(place => {
let div = document.createElement("div");
div.className = "autocomplete-item";
div.textContent = `${place.name}, ${place.state ? place.state + ', ' : ''}${place.country}`;
div.addEventListener("click", () => {
document.getElementById("userLocation").value = place.name;
dropdown.style.display = "none";
finduserLocation();
});
dropdown.appendChild(div);
});
dropdown.style.display = "block";
} else {
dropdown.style.display = "none";
}
})
.catch(error => {
console.error("Error fetching autocomplete data:", error);
dropdown.style.display = "none";
});
}
function finduserLocation(){
Forecast.innerHTML="";
fetch(WEATHER_API_ENDPOINT + userLocation.value)
.then((response)=>response.json())
.then((data)=>{
if(data.cod!='' && data.cod!=200){
alert(data.message);
return;
}
console.log(data);
city.innerHTML=data.name + "," + data.sys.country;
weatherIcon.style.background=`url( https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png)`;
fetch(WEATHER_DATA_ENDPOINT + `lon=${data.coord.lon}&lat=${data.coord.lat}`)
.then((response)=>response.json())
.then((data)=>{
console.log(data);
temperature.innerHTML=Tempconverter(data.current.temp);
feelsLike.innerHTML="Feels like:- " + data.current.feels_like;
description.innerHTML=data.current.weather[0].description;
const options={
weekday: "long", month:"long", day:"numeric", hour: "numeric", minute:"numeric", hour12:true,
};
date.innerHTML=getLongFormatDateTime(data.current.dt,data.timezone_offset,options);
HValue.innerHTML=Math.round(data.current.humidity)+"<span class='units'>%</span>";
WSValue.innerHTML=Math.round(data.current.wind_speed)+"<span class='units'> Km/h</span>";
const options1 = {
hour:"numeric", minute:"numeric", hour12:true,
}
SRValue.innerHTML=" :- " + getLongFormatDateTime(data.current.sunrise,data.timezone_offset,options1);
SSValue.innerHTML=" :- " + getLongFormatDateTime(data.current.sunset,data.timezone_offset,options1);
CValue.innerHTML=Math.round(data.current.clouds)+"<span class='units'>%</span>";
UIValue.innerHTML=Math.round(data.current.uvi);
PValue.innerHTML=Math.round(data.current.pressure)+"<span class='units'> hPa</span>";
data.daily.forEach((weather)=>{
let div = document.createElement("div");
div.className = "sub-div";
const options={
weekday: "long", month:"long", day:"numeric",
};
let daily = getLongFormatDateTime(weather.dt,0,options).split(" at ");
div.innerHTML= daily[0];
div.innerHTML+=`<img src=https://openweathermap.org/img/wn/${weather.weather[0].icon}@2x.png></img>`;
div.innerHTML+=`<p class="forecast-desc">${weather.weather[0].description}</p>`;
div.innerHTML+=`<span><span>${Tempconverter(weather.temp.min)}</span> / <span>${Tempconverter(weather.temp.max)}</span></span>`
Forecast.append(div);
})
});
});
}
function formatUnixTime(dtValue,offSet,options={}){
const date = new Date((dtValue + offSet)*1000);
return date.toLocaleTimeString([],{timeZone:"UTC", ...options});
}
function getLongFormatDateTime(dtValue, offSet, options){
return formatUnixTime(dtValue,offSet,options);
}
function Tempconverter(temp) {
let tempValue = Math.round(Number(temp));
let message = "";
if (typeof converter !== 'undefined' && converter.value) {
if (converter.value == "°C" || converter.value == "°C") {
message = tempValue + "<span> °C</span>";
} else {
let ctof = (tempValue * 9) / 5 + 32;
ctof = Math.round(ctof);
message = ctof + "<span> °F</span>";
}
} else {
console.error("Converter element or its value is not defined.");
}
return message;
}