-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
175 lines (140 loc) · 5.54 KB
/
app.js
File metadata and controls
175 lines (140 loc) · 5.54 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
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
const cityElement=document.querySelector('.city')
const dateElement=document.querySelector('.date')
const tempElement=document.querySelector('.temp')
const weatherElement=document.querySelector('.weather')
const hinlowElement=document.querySelector('.hinlow')
const visibElement=document.querySelector('.visib')
const windElement=document.querySelector('.wind')
var back= document.querySelector(".bg")
// App data
const weather = {};
weather.temperature = {
unit : "celsius"
}
// APP CONSTS AND VARS
const KELVIN = 273;
// API KEY
const key = "0dee913a4ab87358f63a796dc048bc95";
//base url
const base= "https://api.openweathermap.org/data/2.5/"
if('geolocation' in navigator){
navigator.geolocation.getCurrentPosition(setPosition, showerr);
}
else{
cityElement.innerHTML = "Browser doesn't Support Geolocation. ";
}
// SET USER'S POSITION
function setPosition(position){
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
getWeather(latitude, longitude);
}
// SHOW ERROR WHEN THERE IS AN ISSUE WITH GEOLOCATION SERVICE
function showerr(error){
cityElement.innerHTML = ` ${error.message} `;
document.querySelector(".bg").style.backgroundImage = "url('./error.jpg')";
}
// GET WEATHER FROM API PROVIDER
function getWeather(latitude, longitude){
let api = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${key}`;
fetch(api)
.then(function(response){
let data = response.json();
console.log(data)
return data;
})
.then(function(data){
weather.temperature.value = Math.floor(data.main.temp - KELVIN);
weather.description = data.weather[0].description;
if(weather.description=="clear sky"){
document.querySelector(".bg").style.backgroundImage = "url('./clear.jpg')";
}
else if(weather.description=="haze"){
document.querySelector(".bg").style.backgroundImage = "url('./haze.jpg')";
}
else{
document.querySelector(".bg").style.backgroundImage = "url('./else.jpg')";
}
weather.city = data.name;
weather.country = data.sys.country;
weather.visibility=data.visibility;
weather.wind=data.wind.speed;
weather.hi=Math.floor(data.main.temp_max-KELVIN);
weather.low=Math.floor(data.main.temp_min-KELVIN);
})
.then(function(){
displayWeather();
});
}
//calling new function to call api
const searchbox = document.querySelector('.search-box');
searchbox.addEventListener('keypress', setQuery);
function setQuery(evt) {
if (evt.keyCode == 13) {
getResults(searchbox.value);
}
}
function getResults(query){
fetch(`${base}weather?q=${query}&units=metric&APPID=${key}`)
.then(response => {
return response.json();
}).then(function(data){
weather.temperature.value = Math.floor(data.main.temp);
weather.description = data.weather[0].description;
if(weather.description=="haze"){
document.querySelector(".bg").style.backgroundImage = "url('./haze.jpg')";
}
else if(weather.description=="clear sky"){
document.querySelector(".bg").style.backgroundImage = "url('./clear.jpg')";
}
else{
document.querySelector(".bg").style.backgroundImage = "url('./else.jpg')";
}
weather.city = data.name;
weather.country = data.sys.country;
weather.visibility=data.visibility;
weather.wind=data.wind.speed;
weather.hi=Math.floor(data.main.temp_max);
weather.low=Math.floor(data.main.temp_min);
})
.then(function(){
displayWeather();
});
}
//insertion of info in DOM
function displayWeather(){
tempElement.innerHTML = `${weather.temperature.value}°<span>C</span>`;
weatherElement.innerHTML = weather.description;
cityElement.innerHTML = `${weather.city}, ${weather.country} `;
visibElement.innerHTML=` ${weather.visibility} `;
windElement.innerHTML=` ${weather.wind} `;
hinlowElement.innerHTML=`${weather.hi}°c / ${weather.low}°c`
let now = new Date();
dateElement.innerHTML=dateBuilder(now)
}
// C to F conversion
function celsiusToFahrenheit(temperature){
return (temperature * 9/5) + 32;
}
// WHEN THE USER CLICKS ON THE TEMPERATURE ELEMENET
tempElement.addEventListener("click", function(){
if(weather.temperature.value === undefined) return;
if(weather.temperature.unit == "celsius"){
let fahrenheit = celsiusToFahrenheit(weather.temperature.value);
fahrenheit = Math.floor(fahrenheit);
tempElement.innerHTML = `${fahrenheit}°<span>F</span>`;
weather.temperature.unit = "fahrenheit";
}else{
tempElement.innerHTML = `${weather.temperature.value}°<span>C</span>`;
weather.temperature.unit = "celsius"
}
});
function dateBuilder (d) {
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let day = days[d.getDay()];
let date = d.getDate();
let month = months[d.getMonth()];
let year = d.getFullYear();
return `${day} ${date} ${month} ${year}`;
}