-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
161 lines (139 loc) · 6.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(function() {
getTheWeather();
$("body").css("transition", "background-color 1s ease");
$("body").css("background-color", "#000");
}());
function displayWeatherData(weatherReq) {
respXML = weatherReq.responseXML;
console.log(respXML);
var city = respXML.getElementsByTagName("city")[0].attributes["name"].textContent,
country = respXML.getElementsByTagName("country")[0].textContent,
weatherDesc = respXML.getElementsByTagName("weather")[0].attributes["value"].nodeValue,
weatherValue = respXML.getElementsByTagName("weather")[0].attributes["number"].nodeValue,
temperatureValue = Math.floor(respXML.getElementsByTagName("temperature")[0].attributes["value"].textContent),
temperatureMin = Math.floor(respXML.getElementsByTagName("temperature")[0].attributes["min"].nodeValue),
temperatureMax = Math.floor(respXML.getElementsByTagName("temperature")[0].attributes["max"].nodeValue),
temperatureUnit = respXML.getElementsByTagName("temperature")[0].attributes["unit"].nodeValue,
temperatureValueF = Math.floor(temperatureValue * (9 / 5) + 32),
temperatureMinF = Math.floor(temperatureMin * (9 / 5) + 32),
temperatureMaxF = Math.floor(temperatureMax * (9 / 5) + 32),
temperatureContentC = "Temp: " + temperatureValue + " (Min: " + temperatureMin + " Max: " + temperatureMax + ")" + " °C" + " " + '<button type="button" class="btn btn-success" id="toFahrenheit">To °F</button>',
temperatureContentF = "Temp: " + temperatureValueF + " (Min: " + temperatureMinF + " Max: " + temperatureMaxF + ")" + " °F" + " " + '<button type="button" class="btn btn-success" id="toCelsius">To °C</button>',
humidityValue = respXML.getElementsByTagName("humidity")[0].attributes["value"].nodeValue,
humidityUnit = respXML.getElementsByTagName("humidity")[0].attributes["unit"].nodeValue,
windValue = respXML.getElementsByTagName("speed")[0].attributes["value"].nodeValue,
windDirection = respXML.getElementsByTagName("direction")[0].attributes["code"].nodeValue,
windDegrees = respXML.getElementsByTagName("direction")[0].attributes["value"].nodeValue,
pressureValue = respXML.getElementsByTagName("pressure")[0].attributes["value"].nodeValue,
pressureUnit = respXML.getElementsByTagName("pressure")[0].attributes["unit"].nodeValue,
backgroundId = [201, 401, 501, 601, 701, 801],
backgroundIcon = ['thunderstorm',
'sprinkle',
'rain',
'snow',
'fog',
'day-sunny',
'cloudy',
],
backgroundImg = [
'http://tylermoeller.github.io/local-weather-app/assets/img/thunderstorm.jpg',
'https://tylermoeller.github.io/local-weather-app/assets/img/sprinkle.jpg',
'https://tylermoeller.github.io/local-weather-app/assets/img/rain.jpg',
'https://tylermoeller.github.io/local-weather-app/assets/img/snow.jpg',
'https://tylermoeller.github.io/local-weather-app/assets/img/fog.jpg',
'https://tylermoeller.github.io/local-weather-app/assets/img/clear.jpg',
'https://tylermoeller.github.io/local-weather-app/assets/img/cloudy.jpg',
],
iconClass,
bgIndex;
$("#theTitle").html("Free C<i class='wi wi-day-sunny'></i>de Camp");
$("#theSubtitle").html("Weather App");
var mql = window.matchMedia("screen and (max-width: 410px)");
if (mql.matches) { // if media query matches
$("#theTitle").css({
"font-family": "Montserrat, Verdana",
"font-size": "34px"
});
$("#theSubtitle").css({
"font-family": "Quattrocento, Verdana",
"font-size": "26px"
});
} else {
// do something else
$("#theTitle").css({
"font-family": "Montserrat, Verdana",
"font-size": "84px"
});
$("#theSubtitle").css({
"font-family": "Quattrocento, Verdana",
"font-size": "58px"
});
}
$("#city").append(city + " - " + country);
$("#weather").append(weatherDesc);
$("#temp").append(temperatureContentC);
backgroundId.push(weatherValue);
bgIndex = backgroundId.sort().indexOf(weatherValue);
$('body').css({
'background-image': 'url(' + backgroundImg[bgIndex] + ')',
'background-repeat': 'no-repeat',
'background-attachment': 'fixed',
'background-position': 'center'
});
iconClass = backgroundIcon[bgIndex];
$('#icon').html('<i class="wi wi-' + iconClass + '"></i><br>');
$("#humidity").append("Humidity: " + humidityValue + " " + humidityUnit);
$("#wind").append("Wind: " + windValue + " kn " + " - " + windDirection + " " + '<i class="wi wi-wind towards-' + windDegrees + '-deg"></i><br>');
$("#pressure").append("Pressure: " + pressureValue + " " + pressureUnit);
(function convert() {
$("#toFahrenheit").click(function() {
$("#temp").html(temperatureContentF);
$("#toCelsius").click(function() {
$("#temp").html(temperatureContentC);
convert();
return false;
});
return false;
});
}());
}
// Start of geolocation block
function getTheWeather() {
if (navigator.geolocation) {
// timeout at 60000 milliseconds (60 seconds)
var options = {
timeout: 60000,
enableHighAccuracy: true,
maximumAge: 18000
};
navigator.geolocation.getCurrentPosition(callback, errorHandler, options);
} else {
alert('W3C Geolocation API is not available');
}
}
function callback(position) {
var crd = position.coords;
var lat = crd.latitude;
var lon = crd.longitude;
var location = "lat=" + lat + "&" + "lon=" + lon;
var appId = "60d2f49e0004ccad2ad538f264be9564"
var request = "http://api.openweathermap.org/data/2.5/weather?&mode=xml&units=metric&" + location + "&APPID=" + appId;
weatherReq = new XMLHttpRequest();
weatherReq.open("GET", request, false);
console.log(request);
weatherReq.send();
if (weatherReq.status === 200) {
//$(document).ready(displayWeatherData(weatherReq));
displayWeatherData(weatherReq);
} else {
alert("Some error occurred during the request");
}
}
function errorHandler(err) {
if (err.code == 1) {
alert("Error: Access is denied!");
} else if (err.code == 2) {
alert("Error: Position is unavailable!");
}
}
// End of geolocation block