-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (68 loc) · 1.91 KB
/
script.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
const api = "https://fcc-weather-api.glitch.me/api/current?";
const tempUnit = "C";
let lat, lon, currentTempInCelsius;
$(document).ready(() => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
lat = "lat=" + position.coords.latitude;
lon = "lon=" + position.coords.longitude;
getWeather(lat, lon);
});
} else {
console.log("Geolocation is not supported by this browser.");
}
$("#tempunit").click(() => {
const currentTempUnit = $("#tempunit").text();
const newTempUnit = currentTempUnit == "C" ? "F" : "C";
$("#tempunit").text(newTempUnit);
if (newTempUnit == "F") {
const fahTemp = Math.round((parseInt($("#temp").text()) * 9) / 5 + 32);
$("#temp").text(fahTemp + " " + String.fromCharCode(176));
} else {
$("#temp").text(currentTempInCelsius + " " + String.fromCharCode(176));
}
});
});
const getWeather = (lat, lon) => {
const urlString = api + lat + "&" + lon;
$.ajax({
url: urlString,
success: (result) => {
$("#city").text(result.name + ", ");
$("#country").text(result.sys.country);
currentTempInCelsius = Math.round(result.main.temp * 10) / 10;
$("#temp").text(currentTempInCelsius + " " + String.fromCharCode(176));
$("#tempunit").text(tempUnit);
$("#desc").text(result.weather[0].main);
IconGen(result.weather[0].main);
},
});
};
const IconGen = (desc) => {
desc = desc.toLowerCase();
switch (desc) {
case "drizzle":
addIcon(desc);
break;
case "clouds":
addIcon(desc);
break;
case "rain":
addIcon(desc);
break;
case "snow":
addIcon(desc);
break;
case "clear":
addIcon(desc);
break;
case "thunderstom":
addIcon(desc);
break;
default:
$("div.clouds").removeClass("hide");
}
};
const addIcon = (desc) => {
$("div." + desc).removeClass("hide");
};