-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (157 loc) · 6.83 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={b7fa37c888c785bb1148d337aa76e53e}
// https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API KEY}
// https://api.openweathermap.org/data/2.5/weather?q=sangli&appid=b7fa37c888c785bb1148d337aa76e53e
//function to get data by fetch
function getData(){
let body=document.getElementById("error");
body.innerHTML=null;
let maincontainer=document.getElementById("main_container");
let daily=document.getElementById("daily");
maincontainer.style.visibility="visible"
daily.style.visibility="visible";
let city=document.getElementById("query").value;
let url=`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=b7fa37c888c785bb1148d337aa76e53e`;
fetch(url).then(function(res){
// console.log(res);
let data=res.json();
// console.log(data);
return data;
//or return res.json();
}).then(function(res){
console.log(res)
append(res);
//pass all data to the append function
}).catch(function(err){
console.log(err);
//when error is coming add this image to error div
let banner=document.createElement("img");
banner.src="https://mir-s3-cdn-cf.behance.net/project_modules/disp/af8a6a32739675.5691dd2389a24.gif";
banner.id="banner";
let body=document.getElementById("error");
body.append(banner)
let maincontainer=document.getElementById("main_container");
let daily=document.getElementById("daily");
maincontainer.style.visibility="hidden"
daily.style.visibility="hidden";
})
//.catch is not mandetory
}
//this function is for appending data
function append(data){
let container=document.getElementById("container");
container.innerHTML=null;
let h2=document.createElement("h2");
h2.innerText=data.name;
let temp=document.createElement("p");
temp.innerText=`🌡️ Temp: ${data.main.temp}`;
let min_temp=document.createElement("p");
min_temp.innerText=`Min Temp: ${data.main.temp_min}`;
let max_temp=document.createElement("p");
max_temp.innerText=`Min Temp: ${data.main.temp_max}`;
let wind=document.createElement("p");
wind.innerText=`🪂 Winds:- Speed:${data.wind.speed}, Deg:${data.wind.deg}`;
let clouds=document.createElement("p");
clouds.innerText=`☁️ Clouds: ${data.weather[0].description}`
//sunrise - unix time conversion to normal time
let unix_timestamp = data.sys.sunrise;
var date = new Date(unix_timestamp * 1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2)+(" IST");
console.log(formattedTime);
let sunrise=document.createElement("p");
sunrise.innerText=`🌅 Sunrise: ${formattedTime}`
//sunset - unix time to normal time conversion
let unix_timestamp2 = data.sys.sunset;
var date2 = new Date(unix_timestamp2 * 1000);
// Hours part from the timestamp
var hours2 = date2.getHours();
// Minutes part from the timestamp
var minutes2 = "0" + date2.getMinutes();
// Seconds part from the timestamp
var seconds2 = "0" + date2.getSeconds();
// Will display time in 10:30:23 format
var formattedTime2 = hours2 + ':' + minutes2.substr(-2) + ':' + seconds2.substr(-2)+(" IST");
console.log(formattedTime2);
let sunset=document.createElement("p");
sunset.innerText=`🌇 Sunset: ${formattedTime2}`
//append all data to the container
container.append(h2,temp,min_temp,max_temp,wind,clouds,sunrise,sunset);
//map part
let url=`https://maps.google.com/maps?q=${data.name}&t=&z=13&ie=UTF8&iwloc=&output=embed`
let iframe=document.getElementById("gmap_canvas");
iframe.src=url;
//7 days weather part
let lat=data.coord.lat;
let lon=data.coord.lon;
// console.log(lat,lon)
dailydata(lat,lon);
//now collect data by fetching with api
async function dailydata(lat,lon){
try{
let res=await fetch(`https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=hourly,minutely&appid=b7fa37c888c785bb1148d337aa76e53e&units=metric`);
let data=await res.json();
console.log(data)
appendDaily(data);
}catch(err){
// console.log("error");
}
}
function appendDaily(data){
let daily=document.getElementById("daily");
daily.innerHTML=null;
let div = document.createElement("div");
div.id = "divbox"
for(let i=0; i<7; i++){
let day = document.createElement("div");
day.setAttribute("class","day")
let getDate=new Date(data.daily[i].dt * 1000);
let date=(getDate.toDateString())
let png = document.createElement("img");
png.src = `http://openweathermap.org/img/wn/${data.daily[i].weather[0].icon}@2x.png`
png.class = "png"
console.log(data.daily[i].weather[0].icon)
let max_temp = document.createElement("p");
max_temp.innerText = `${data.daily[i].temp.max} °C`;
let min_temp = document.createElement("p");
min_temp.innerText = `${data.daily[i].temp.min} °C`;
day.append(date,png,max_temp,min_temp);
div.append(day);
}
daily.append(div)
}
}
function getLocation(){
navigator.geolocation.getCurrentPosition(success);
function success(pos) {
const crd = pos.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
getWeatherOnLocation(crd.latitude,crd.longitude)
}
}
// getLocation(); -->in body Onload
function getWeatherOnLocation(lat,lon){
let city=document.getElementById("query").value;
let url=`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=b7fa37c888c785bb1148d337aa76e53e`
//let url=`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=b7fa37c888c785bb1148d337aa76e53e`;
fetch(url).then(function(res){
// console.log(res);
let data=res.json();
// console.log(data);
return data;
//or return res.json();
}).then(function(res){
console.log(res)
append(res)
}).catch(function(err){
console.log("get location error");
})
}