-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (115 loc) · 4.87 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
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
let wheather ={
apiKey : "a33001df1c319dd29dbb5895c1b60cfe",
getdatalonlat : function (city){
fetch(
"//api.openweathermap.org/geo/1.0/direct?q="+city+"&limit=5&appid=a33001df1c319dd29dbb5895c1b60cfe"
)
.then((response) => response.json())
.then((data) => this.getLanLon(data))
},
getLanLon: function (data){
const {lat} = data[0];
const {lon} = data[0];
this.getWeather(lat,lon);
this.getsunrisesunset(lat,lon);
this.getPlaceName(lat,lon);
},
getWeather : function (lat,lon){
fetch("https://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+lon+"&exclude=&units=metric&appid=a33001df1c319dd29dbb5895c1b60cfe")
.then((response) => response.json())
.then((data) => this.wheather_2(data))
},
wheather_2 : function (data){
const {humidity,feels_like} = data.main;
const {name} = data;
const {main} = data.weather[0];
const {speed} = data.wind;
document.querySelector(".hum-per").innerHTML = humidity + "%";
document.querySelector(".celsius").innerHTML = feels_like + '\u00B0';
document.querySelector(".wind-value").innerHTML = speed+ " km/h";
},
search: function () {
this.getdatalonlat(document.querySelector(".search").value);
},
getsunrisesunset : function(lat,lon){
fetch("https://api.sunrisesunset.io/json?lat="+lat+"&lng="+lon+"&timezone=IST")
.then((response) => response.json())
.then((data) => this.setsunsrisesunset(data))
},
setsunsrisesunset: function(data){
const {sunrise} = data.results;
const {sunset} = data.results;
document.querySelector(".time-2").innerHTML = sunset.slice(0,4)+ " pm";
document.querySelector(".time-3").innerHTML = sunrise.slice(0,4) + " am";
},
getPlaceName: function (lat,lon){
fetch("https://api.geoapify.com/v1/geocode/reverse?lat="+lat+"&lon="+lon+"&apiKey=0d47df981dc843aaa30cd5fe7adc7f71")
.then((response) => response.json())
.then((data) => this.placeName(data))
},
placeName: function(data){
const {country} = data.features[0].properties;
const {city} = data.features[0].properties;
document.querySelector(".text-1").innerHTML = city+','+country;
}
}
document.querySelector(".search-button").addEventListener("click",function(){
wheather.search();
})
document.querySelector(".search").addEventListener("keyup", function (event){
if (event.key == "Enter"){
wheather.search();
}
})
let d = new Date();
let dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
let day = dayNames[d.getDay()];
let hour = d.getHours();
let min = d.getMinutes();
let ampm = hour >= 12?"pm":"am";
document.querySelector(".day").innerHTML = day;
document.querySelector(".time").innerHTML = hour + ":" + min +" "+ ampm;
if (ampm === "am" && hour >6 || ampm === "pm" && hour > 18){
let img = document.querySelector(".weather-image");
img.src = "sun-512.png";
img.classList.add("weather-image-day");
}
if (hour>=6 && hour<18){
document.querySelector(".display").style.backgroundImage = "url('Sunrise image.jpg')";
document.body.style.background = "#9B144E"
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
nowgetNowplace(lat,lon);
getChangeDeg(lat,lon);
});
} else {
console.log("Geolocation is not supported by this browser.");
}
function getChangeDeg(lat,lon){
fetch("https://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+lon+"&exclude=&units=metric&appid=a33001df1c319dd29dbb5895c1b60cfe")
.then((response) => response.json())
.then((data) => this.changeDeg(data))
}
function changeDeg(data){
const {humidity,feels_like} = data.main;
const {name} = data;
const {main} = data.weather[0];
const {speed} = data.wind;
document.querySelector(".hum-per").innerHTML = humidity + "%";
document.querySelector(".celsius").innerHTML = feels_like + '\u00B0';
document.querySelector(".wind-value").innerHTML = speed+ " km/h";
}
function nowgetNowplace(lat,lon){
fetch("https://api.geoapify.com/v1/geocode/reverse?lat="+lat+"&lon="+lon+"&apiKey=0d47df981dc843aaa30cd5fe7adc7f71")
.then((response) => response.json())
.then((data) => this.nowPlaceName(data))
}
function nowPlaceName(data){
const {country} = data.features[0].properties;
const {city} = data.features[0].properties;
document.querySelector(".text-1").innerHTML = city+','+country;
}
getplacenonce()