-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (48 loc) · 2.38 KB
/
index.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
window.addEventListener('load' , ()=>{ // here function will be called after the screen loads
let long;
let lat;
let temperatureDescription = document.querySelector(".temperature-description");
let temperatureDegree = document.querySelector(".temperature-degree");
let locationTimezone = document.querySelector(".location-timezone");
let locationIcon = document.getElementById("img-icon");
let temperatureSection = document.querySelector('.degree-section');
let temperatureSpan = document.querySelector('.temperature span');
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(position => {
long= position.coords.longitude;
lat = position.coords.latitude;
const api = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=579a56d7c538e57995344ec83941c759`;
fetch(api)
.then(response => { // it just means ki api fetch hone ke badd hi kaam chalu hoga.
return response.json();
})
.then(data => {
console.log(data);
const temp = Math.floor((data.main.temp)-273.15);
const description = data.weather[0].main;
const cityName = data.name;
const icon = data.weather[0].icon;
//formulla
let fahrenheit = Math.floor(temp*(9/5) + 32);
temperatureDegree.textContent = temp ;
temperatureDescription.textContent = description;
locationTimezone.textContent = cityName;
locationIcon.src = `icons/${icon}.png`;
//change temp unit
temperatureSection.addEventListener("click", () => {
if(temperatureSpan.textContent === '°F') {
temperatureSpan.textContent = "°C";
temperatureDegree.textContent = temp;
} else{
temperatureSpan.textContent = "°F";
temperatureDegree.textContent = fahrenheit;
}
})
})
});
}
// else{
// h1.textContent ="Hey this is not working because ..."
// }
})
//api key = 579a56d7c538e57995344ec83941c759