-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
52 lines (45 loc) · 1.88 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
var api = "https://fcc-weather-api.glitch.me/api/current?";
var lat, lon;
var tempCelcius;
var tempFahrenheit;
function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showData);
}
else{
document.getElementById("userCity").innerHTML = "Unable to retrieve location";
}
}
function showData(position) {
var lat= position.coords.latitude;
var lon= position.coords.longitude;
console.log(lat);
console.log(lon);
var apiUrl = api + "lon=" + lon + "&lat=" + lat;
console.log(apiUrl);
$.getJSON(apiUrl, function(json){
console.log(json);
var city=json.name;
var country=json.sys.country;
tempCelcius=json.main.temp;
tempFahrenheit=(tempCelcius * 9/5 +32).toFixed(2);
var weatherDesc = json.weather[0].description;
document.getElementById("userLocation").innerHTML = city+", "+country;
document.getElementById("temperature").innerHTML = tempCelcius + " " +String.fromCharCode(176) + "C";
document.getElementById("weatherDescription").innerHTML = weatherDesc;
document.getElementById("weatherIcon").className="wi wi-owm-"+json.weather[0].id;
document.getElementsByTagName("html")[0].style.visibility = "visible";
return displayTemp, tempCelcius, tempFahrenheit;
})}
function displayCelcius(){
var displayTemp= tempCelcius;
document.getElementById("temperature").innerHTML = displayTemp + " " +String.fromCharCode(176) + "C";
document.getElementById("c-button").className="btn btn-primary active";
document.getElementById("f-button").className="btn btn-default";
}
function displayFahrenheit(){
var displayTemp= tempFahrenheit;
document.getElementById("temperature").innerHTML = displayTemp + " " +String.fromCharCode(176) + "F";
document.getElementById("f-button").className="btn btn-primary active";
document.getElementById("c-button").className="btn btn-default";
}