-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (89 loc) · 2.68 KB
/
app.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
var APPID = "a593acda2e564a8ffba201992e1f89f9";
var temp;
var loc;
var icon;
var humidity;
var wind;
var direction;
function update(weather) {
icon.src = "img/codes/" + weather.code + ".png"
humidity.innerHTML = weather.humidity;
wind.innerHtml = weather.wind;
direction.innerHTML = weather.direction;
loc.innerHTML = weather.location;
temp.innerHTML = weather.temp;
}
window.onload = function () {
temp = document.getElementById("temperature");
loc = document.getElementById("location");
icon = document.getElementById("icon");
humidity = document.getElementById("humidity");
wind = document.getElementById("wind");
direction = document.getElementById("direction");
/* NEW */
if(navigator.geolocation){
var showPosition = function(position){
updateByGeo(position.coords.latitude, position.coords.longitude);
}
navigator.geolocation.getCurrentPosition(showPosition);
} else {
var zip = window.prompt("Could not discover your location. What is your zip code?");
updateByZip(zip);
}
}
function updateByGeo(lat, lon){
var url = "http://api.openweathermap.org/data/2.5/weather?" +
"lat=" + lat +
"&lon=" + lon +
"&APPID=" + APPID;
sendRequest(url);
}
function updateByZip(zip){
var url = "http://api.openweathermap.org/data/2.5/weather?" +
"zip=" + zip +
"&APPID=" + APPID;
sendRequest(url);
}
function sendRequest(url){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
var weather = {};
weather.code = data.weather[0].id;
weather.humidity = data.main.humidity;
weather.wind = KHP(data.wind.speed);
weather.direction = degreesToDirection(data.wind.deg)
weather.location = data.name;
weather.temp = K2C(data.main.temp);
update(weather);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function degreesToDirection(degrees){
var range = 360/16;
var low = 360 - range/2;
var high = (low + range) % 360;
var angles = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
for( i in angles ) {
if(degrees >= low && degrees < high){
console.log(angles[i]);
return angles[i];
console.log("derp");
}
low = (low + range) % 360;
high = (high + range) % 360;
}
return "N";
}
function K2F(k){
return Math.round(k*(9/5)-459.67);
}
function K2C(k){
return Math.round(k - 273.15);
}
function KHP(k){
return Math.round(k / 1.609344);
}