-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
129 lines (103 loc) · 4.58 KB
/
weather.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
const apikey="6b1bed77bd64be2e10ff3010348fdf51";
window.addEventListener("load",()=>{
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition((position)=>{
let lon= position.coords.longitude;
let lat= position.coords.latitude;
const url= `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&` + `lon=${lon}&appid=${apikey}`;
fetch(url).then((res)=>{
return res.json();
}).then((data)=>{
console.log(data);
console.log(new Date().getTime())
var dat= new Date(data.dt)
console.log(dat.toLocaleString(undefined,'Asia/Kolkata'))
console.log(new Date().getMinutes())
weatherReport(data);
})
})
}
})
function searchByCity(){
var place= document.getElementById('input').value;
var urlsearch= `http://api.openweathermap.org/data/2.5/weather?q=${place}&` + `appid=${apikey}`;
fetch(urlsearch).then((res)=>{
return res.json();
}).then((data)=>{
console.log(data);
weatherReport(data);
})
fetch(urlsearch).then((res)=>{
return res.json();
}).then((data)=>{
console.log(data);
// lat = data[0].lat;
// lon = data[0].lon;
weatherReport(data);
})
document.getElementById('input').value='';
}
function weatherReport(data){
var urlcast= `http://api.openweathermap.org/data/2.5/forecast?q=${data.name}&` + `appid=${apikey}`;
fetch(urlcast).then((res)=>{
return res.json();
}).then((forecast)=>{
console.log(forecast.city);
hourForecast(forecast);
dayForecast(forecast)
console.log(data);
document.getElementById('city').innerText= data.name + ', '+data.sys.country;
console.log(data.name,data.sys.country);
console.log(Math.floor(data.main.temp-273));
document.getElementById('temperature').innerText= Math.floor(data.main.temp-273)+ ' °C';
document.getElementById('clouds').innerText= data.weather[0].description;
console.log(data.weather[0].description)
let icon1= data.weather[0].icon;
let iconurl= "http://api.openweathermap.org/img/w/"+ icon1 +".png";
document.getElementById('img').src=iconurl
})
}
function hourForecast(forecast){
document.querySelector('.templist').innerHTML=''
for (let i = 0; i < 5; i++) {
var date= new Date(forecast.list[i].dt*1000)
console.log((date.toLocaleTimeString(undefined,'Asia/Kolkata')).replace(':00',''))
let hourR=document.createElement('div');
hourR.setAttribute('class','next');
let div= document.createElement('div');
let time= document.createElement('p');
time.setAttribute('class','time')
time.innerText= (date.toLocaleTimeString(undefined,'Asia/Kolkata')).replace(':00','');
let temp= document.createElement('p');
temp.innerText= Math.floor((forecast.list[i].main.temp_max - 273))+ ' °C' + ' / ' + Math.floor((forecast.list[i].main.temp_min - 273))+ ' °C';
div.appendChild(time)
div.appendChild(temp)
let desc= document.createElement('p');
desc.setAttribute('class','desc')
desc.innerText= forecast.list[i].weather[0].description;
hourR.appendChild(div);
hourR.appendChild(desc)
document.querySelector('.templist').appendChild(hourR);
}
}
function dayForecast(forecast){
document.querySelector('.weekF').innerHTML=''
console.log(forecast.list);
for (let i = 5; i < forecast.list.length; i+=8) {
console.log(forecast.list[i]);
let div= document.createElement('div');
div.setAttribute('class','dayF');
let day= document.createElement('p');
day.setAttribute('class','date')
day.innerText= new Date(forecast.list[i].dt*1000).toDateString(undefined,'Asia/sikkim');
div.appendChild(day);
let temp= document.createElement('p');
temp.innerText= Math.floor((forecast.list[i].main.temp_max - 273))+ ' °C' + ' / ' + Math.floor((forecast.list[i].main.temp_min - 273))+ ' °C';
div.appendChild(temp)
let description= document.createElement('p');
description.setAttribute('class','desc')
description.innerText= forecast.list[i].weather[0].description;
div.appendChild(description);
document.querySelector('.weekF').appendChild(div)
}
}