-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (78 loc) · 4.08 KB
/
script.js
File metadata and controls
94 lines (78 loc) · 4.08 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
let form = document.querySelector("form");
let getUserLocation = (finalcity = null) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
async (position) => {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
let weatherWrapper = document.querySelector(".InsideDetails");
let currentlocApi = async () => {
let weatherDetails = await fetch(`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`);
let finalDetails = await weatherDetails.json();
return finalDetails;
};
let finalDetails = await currentlocApi();
if (!finalcity) finalcity = finalDetails.address.city;
let response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${finalcity}&appid=0f2fb94282ad6a3dbf2387c407b74806&units=metric`);
let Data = await response.json();
let { name, main, weather, wind } = Data;
// let inputField = document.querySelector("input[name='CityName']");
// inputField.value = inputField.value + `${name}`;
let weatherItems = `
<div class="weather-Details">
<div class="circle">
<figure>
<img src="http://openweathermap.org/img/w/${weather[0].icon}.png" alt="weather">
</figure>
<div class="temprature">
<h2>${main.temp} <sup>°</sup></h2>
<span class="cityName">${name}</span>
</div>
<div class="MoreAboutWeater">
<p>Wind now<span>${wind.speed}</span></p>
<p>Humidity <span>${main.humidity}</span></p>
</div>
</div>
</div>`;
weatherWrapper.innerHTML = weatherItems;
},
(error) => {
console.error("Error fetching location:", error.message);
form = document.querySelector("form");
form.addEventListener("submit", async (event) => {
event.preventDefault();
let CityName = event.target.CityName.value;
let weatherWrapper = document.querySelector(".InsideDetails");
let response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${CityName}&appid=0f2fb94282ad6a3dbf2387c407b74806&units=metric`);
let Data = await response.json();
let { name, main, weather, wind } = Data;
let weatherItems = `
<div class="weather-Details">
<div class="circle">
<figure>
<img src="http://openweathermap.org/img/w/${weather[0].icon}.png" alt="weather">
</figure>
<div class="temprature">
<h2>${main.temp} <sup>°</sup></h2>
<span class="cityName">${name}</span>
</div>
<div class="MoreAboutWeater">
<p>Wind now<span>${wind.speed}</span></p>
<p>Humidity <span>${main.humidity}</span></p>
</div>
</div>
</div>`;
weatherWrapper.innerHTML = weatherItems;
});
}
);
} else {
console.log("Geolocation is not supported by this browser.");
}
};
getUserLocation();
form.addEventListener("submit", async (event) => {
event.preventDefault();
let finalcity = event.target.CityName.value;
getUserLocation(finalcity);
})