-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (64 loc) · 2.17 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
$("document").ready(function () {
$("#weather").hide();
$("#error").hide();
if (navigator.geolocation) {
var lat;
var lon;
navigator.geolocation.getCurrentPosition(function(position) {
console.log(position);
lat = position.coords.latitude;
lon = position.coords.longitude;
var url = "https://fcc-weather-api.glitch.me/api/current?lat=" + lat + "&lon="+lon;
$.getJSON(url, function(json) {
var celcius = true;
var tempC = json.main.temp;
var tempF = tempC * 9 / 5 + 32;
tempF = Number(tempF.toString().slice(0, 5));
$("#loading").hide();
$("#icon").attr("src", json.weather[0].icon);
$("#main").html(json.weather[0].main);
$("#description").html(json.weather[0].description);
$("#location").html(json.name + ", " + json.sys.country);
$("#temp").html(tempC + " ℃");
$("#changeUnit").html("Change to Farenheit");
$("#changeUnit").on("click", function(){
var button = this;
if(celcius) {
$("#temp").html(tempF + " ℉");
$("#changeUnit").text("Change to Celcius");
celcius = false;
} else {
$("#temp").html(tempC + " ℃");
$("#changeUnit").text("Change to Farenheit");
celcius = true;
}
});
$("#weather").fadeIn("slow");
console.log(json);
});
}, function(error){
switch(error.code) {
case error.PERMISSION_DENIED:
showError("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
showError("Your location information is unavailable.");
break;
case error.TIMEOUT:
showError("The request to get your location timed out.");
break;
case error.UNKNOWN_ERROR:
showError("An unknown error occurred.");
break;
}
});
} else {
showError("Your browser does not support Geolocation!");
}
});
function showError(error) {
$("#loading").hide();
$("h1").css("color", "red");
$("#error").html(error);
$("#error").fadeIn("slow");
}