-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
98 lines (82 loc) · 3.04 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
$(document).ready(function() {
if(!navigator.geolocation){
output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
return;
}
/**
* Get you location when you allowed the prompt
*/
navigator.geolocation.getCurrentPosition(function(position) {
var point = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
new google.maps.Geocoder().geocode({'latLng': point}, function(res, status) {
if(status === 'OK')
getWeather(position.coords.latitude, position.coords.longitude);
$(".city").html("<h2>"+res[0].address_components[3].long_name+"</h2>");
});
}, function(err) {
console.log(err);
});
/**
* Get the weather (api fc weather) from your geolocation and show info to html page
*
* @param {float} lat Latitud of your location
* @param {float} lng Longitude of your location
*/
function getWeather(lat, lng) {
var requestString = "https://fcc-weather-api.glitch.me//api/current?lat="+lat+"&lon="+lng;
$.ajax({
type: 'GET',
url: requestString,
success: function(response) {
$(".temperature").html("<h4>"+Math.round(response.main.temp)+" ℃</h4>");
$(".description").html("<p>"+response.weather[0].description+"</p><span><img src='"+response.weather[0].icon+"'/></span>");
$(".sunrise-sunset").html("<p>Sunrise: "+convertTimestamp(response.sys.sunrise)+"</p><p>Sunset: "+convertTimestamp(response.sys.sunset)+"</p>");
},
error: function(err) {
console.log(err);
}
})
}
/**
* Change the temperature scale to fahrenheit o celsius
*/
$(".temperature").on("click", function(evt) {
var text = evt.target.innerText;
text = text.split(' ');
if(text[1] === '℃') {
var fahrenheit = Math.round(toFahrenheit(text[0]));
$(".temperature").html("<h4>"+fahrenheit+" ℉</h4>");
} else if([text[1] === '℉']) {
var celsius = Math.round(toCelsius(text[0]));
$(".temperature").html("<h4>"+celsius+" ℃</h4>");
}
})
/**
* Convert a temperature value from celsius to fahrenheit
*
* @param {number} celsius the temperature in celsius scale
* @return {number} The temperature in fahrenheit scale
*/
function toFahrenheit(celsius) { return ((celsius * 1.8) + 32); }
/**
* Convert a temperature value from fahrenheit to celsius
*
* @param {number} fahrenheit the temperature in fahrenheit scale
* @return {number} The temperature in celsius scale
*/
function toCelsius(fahrenheit) { return ((fahrenheit - 32) * (5/9)); }
/**
* Convert Unix time to formated time
*
* @param {number} timestamp Unix format to represent time
* @return {string} formattedTime time with normal user representation
*/
function convertTimestamp(timestamp) {
var date = new Date(timestamp * 1000);
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
return formattedTime;
}
});