-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
113 lines (77 loc) · 3.98 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
108
109
110
111
112
$(function(){
var key = "e72e06baca307df8ed39aebe3244ad3d";
var location = {
}
function getLocation(){
if(!navigator.geolocation){
alert("What could possibly be wrong")
}
function success(position){
var long = position.coords.longitude;
var lat = position.coords.latitude;
var url = "https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat="+lat+ "&lon=" + long +"&appid=" + key ;
function displayLocation(){
$.ajax({
url: url,
success: function(data){
$("#locateApp").html(data.name + ", " + data.sys.country);
//$("#tempApp").html(data.main.temp)
/*function convertToCelsius(dataTime){
var inKelvin = dataTime;
var zeroKelvin = 273.15;
var convert = inKelvin - zeroKelvin;
return convert + "℃";
}
//convertToCelsius(kelvin)
$("#tempApp").html(convertToCelsius(data.main.temp));*/
var metric ={
temp: data.main.temp,
celsius: "℃",
fahr: "℉",
toCelsius: function(){
var convert = this.temp- 273.15;
return Math.ceil(convert);
},
toFahr: function(){
var convert = (this.temp * 1.8)- 459.67;
return Math.ceil(convert);
}
}
function toggleWeather(){
$("#tempApp").html(metric.toCelsius() + metric.celsius);
var div = document.getElementById("tempApp");
div.addEventListener("click", function(){
var div = document.getElementById('tempApp');
var sliced = metric.celsius.slice(3);
var num = parseInt(sliced, 16);
var str = String.fromCharCode(num);
var slicedFahr = metric.fahr.slice(3);
var numFahr = parseInt(slicedFahr, 16);
var strFahr = String.fromCharCode(numFahr);
if (div.innerHTML === metric.toCelsius() + str) {
div.innerHTML = metric.toFahr() + metric.fahr;
}
else if(div.innerHTML === metric.toFahr() + strFahr){
div.innerHTML = metric.toCelsius() + metric.celsius
}
else {
console.log('No match string');
console.log(metric.toCelsius() + str);
}
}, false)
}
toggleWeather()
$("#icon").html("<img src='" +"https://openweathermap.org/img/w/" + data.weather[0].icon +"." + "png" + "'>");
//convertToFahr
}
})
}
displayLocation()
};
function error(){
alert("can't access location")
}
navigator.geolocation.getCurrentPosition(success,error)
}
getLocation();
});