-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
168 lines (153 loc) · 5.07 KB
/
javascript.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
try {
navigator.geolocation.getCurrentPosition(function(pos) {
var lat = pos.coords.latitude;
var long = pos.coords.longitude;
// Test various locations
// lat = 40.728373;
// long = -74.172490;
getWeatherData(lat, long);
});
} catch (e) {
alert("Your browser does not support geolocation services.");
}
// Click Listeners
$("#F").click(function() {
if ($("#F").hasClass("selected") == 0) {
$(this).addClass("selected");
$("#C").removeClass("selected");
var celcius = $("#temp").text();
$("#temp").text((celcius * (9 / 5) + 32).toFixed(0));
}
});
$("#C").click(function() {
if ($("#C").hasClass("selected") == 0) {
$(this).addClass("selected");
$("#F").removeClass("selected");
var fahrenheit = $("#temp").text();
$("#temp").text((((fahrenheit - 32) * 5) / 9).toFixed(1));
}
});
// Functions
function updateWeatherData(temp, city, country) {
if ($("#F").hasClass("selected")) {
$("#temp").text((1.8 * (temp - 273) + 32).toFixed(0));
} else if ($("#C").hasClass("selected")) {
$("#temp").text((temp - 273.15).toFixed(1));
}
$("#location").text(city + ", " + country);
}
function updateWeatherBackground(
weatherPrimary,
weatherSecondary,
sunriseTime,
sunsetTime
) {
var curDate = Date.now() / 1000; // Divide by 1000 to get time in seconds
if (curDate > sunriseTime && curDate < sunsetTime) {
dayTime = true;
$("body").css("background", "rgb(86, 149, 191)");
} else {
dayTime = false;
$("body").css("background", "rgb(28, 39, 51)");
$("button").toggleClass("night-time");
}
if (weatherPrimary == "Clear") {
$("#weather-text").text("clear");
if (dayTime) {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522848/danieledesantis-weather-icons-sunny_ifhcu4.svg"
);
} else {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522842/danieledesantis-weather-icons-night-clear_s03q48.svg"
);
}
} else if (weatherPrimary == "Clouds") {
if (weatherSecondary == "broken clouds") {
$("#weather-text").text("partly cloudy");
if (dayTime) {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522849/danieledesantis-weather-icons-cloudy_may53x.svg"
);
} else {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522842/danieledesantis-weather-icons-night-cloudy_faqwcw.svg"
);
}
} else {
$("#weather-text").text("cloudy");
if (dayTime) {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522850/danieledesantis-weather-icons-cloudy-3_lx1nzr.svg"
);
} else {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522842/danieledesantis-weather-icons-night-cloudy-3_knuv8o.svg"
);
}
}
} else if (weatherPrimary == "Rain") {
$("#weather-text").text("rainy");
if (dayTime) {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522845/danieledesantis-weather-icons-rainy_uqq06h.svg"
);
} else {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522842/danieledesantis-weather-icons-night-rainy_nduxzx.svg"
);
}
} else if (weatherPrimary == "Snow") {
$("#weather-text").text("snowy");
if (dayTime) {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522845/danieledesantis-weather-icons-snowy_bo2ghg.svg"
);
} else {
$("img").attr(
"src",
"https://res.cloudinary.com/aeroware/image/upload/v1497522845/danieledesantis-weather-icons-night-snowy_uqzbcz.svg"
);
}
} else {
$("#weather-text").text("unknown");
// Insert question mark image
}
}
function getWeatherData(lat, lon) {
const openweathermapkey = "702be017e2a96887878d8cc987da071c";
var api = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&APPID=${openweathermapkey}`;
$.getJSON(api, {
format: "json"
})
.done(function(response) {
var temp = response.main.temp; // [K]
var sunriseTime = new Date(response.sys.sunrise); // unix, UTC [s]
var sunsetTime = new Date(response.sys.sunset); // unix, UTC [s]
var weatherPrimary = response.weather[0].main; // [-]
var weatherSecondary = response.weather[0].description; // [-]
var city = response.name; // [-]
var country = response.sys.country; // [-]
updateWeatherData(temp, city, country);
updateWeatherBackground(
weatherPrimary,
weatherSecondary,
sunriseTime,
sunsetTime
);
$("body").css("display", "block");
return 1;
})
.fail(function() {
alert("API call failed");
});
}