-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
107 lines (101 loc) · 3.02 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
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
var WEATHER_API_URL = 'https://fcc-weather-api.glitch.me/api/current';
var html = "",
city = "",
region = "",
country = "",
loc = "",
lon = "",
lat = "";
var convert = function (C) {
var F;
F = (C * (9 / 5)) + 32;
return F.toFixed(2);
};
var toFahrenheit = function () {
getLocation;
var y = document.getElementsByClassName("unit");
var i;
for (i = 0; i < y.length; i++) {
y[i].innerHTML = '℉';
}
$('#ButtonC').removeClass('btn-primary');
$('#ButtonF').addClass('btn-primary');
$('#ButtonC').addClass('btn-default');
};
var toCelsius = function () {
getLocation;
var y = document.getElementsByClassName("unit");
var i;
for (i = 0; i < y.length; i++) {
y[i].innerHTML = '℃';
}
$('#ButtonF').removeClass('btn-primary');
$('#ButtonC').addClass('btn-primary');
$('#ButtonF').addClass('btn-default');
};
var findWeather = function () {
if ($('#ButtonC').hasClass('btn-primary')) {
toCelsius();
} else if ($('#ButtonF').hasClass('btn-primary')) {
toFahrenheit();
}
};
var getLocation = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getWeatherData);
}else{
console.log("Geo Location not supported");
}
};
var getWeatherData = function (locationData) {
var lat = locationData['coords']['latitude'],
lon = locationData['coords']['longitude'];
$.ajax({
url: WEATHER_API_URL,
data: {
lat: lat,
lon: lon
},
type: 'GET',
dataType: 'json',
success: renderData
});
}
var renderData = function (data) {
console.log(data)
var temp = data['main']['temp'],
weather_type = data['weather'][0]['main'],
weather_desc = data['weather'][0]['description'],
icon = data['weather'][0]['icon'],
iso_date = new Date(data['dt'] * 1000),
date = iso_date.toLocaleDateString(),
time_updated = iso_date.toLocaleTimeString(),
pressure = data['main']['pressure'],
humidity = data['main']['humidity'],
sunrise = new Date(data['sys']['sunrise'] * 1000).toLocaleTimeString(),
sunset = new Date(data['sys']['sunset'] * 1000).toLocaleTimeString(),
lat = data['coord']['lat'],
lon = data['coord']['lon'],
windSpeed = data['wind']['speed'],
inclination = data['wind']['deg'];
html = '<pre><b>Weather </b>:<b>' + weather_type + '</b>, ' + weather_desc +
'<br><b>Location </b>:<b>' + city + ' ' + data['name'] + ',<b> ' + country + '(' + data['sys']['country'] + ')' + '</b>' +
'<br/><b>Humidity </b>:' + humidity + '%' +
'<br/><b>Pressure </b>: ' + pressure + ' kPa' +
'<br/><b>Date Updated </b>: ' + date +
'<br/><b>Sunrise </b>: ' + sunrise +
'<br/><b>Sunset </b>: ' + sunset +
'<br/><b>Latitude </b>:' + lat +
'<br/><b>Longitude </b>:' + lon +
'<br/><b>Wind Speed</b>:' + windSpeed + ' m/s at ' + inclination + 'deg</pre>';
$('#location').html(html);
$('#temp-main').html(temp + ' (℃)');
$('#weather').text(weather_type);
$('#icon').find("img").attr({
src: icon
});
console.log(html);
}
$(function () {
$(getLocation);
});