-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoWeather.js
206 lines (160 loc) · 6.46 KB
/
geoWeather.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
gets position by geolocation api
then reverse geocoding using google maps api
then asking google weather api for current weather condition and temperature
*/
var position; // computed position object
var browserSupportsGeolocation; // flag for browser support
var time; // start time (constant) for timestamps
var timeoutCity;
var timeoutWeather;
/* HTML Elements*/
var cityname;
var temperature;
var condition;
var language; // language (e.g. 'en')
/* helpers */
var langstrings;
var tempInnerHTML;
function init() { // let's go!
/* init timer */
var d = new Date();
time = d.getTime();
timestamp("Initializing...");
initLanguageStrings();
/* initialize variables */
language = document.documentElement.getAttribute("lang");
cityname = document.getElementById("cityname");
temperature = document.getElementById("temperature");
condition = document.getElementById("condition");
/* hide error textnodes and show progress indicator */
cityname.style.visibility = "hidden";
tempInnerHTML = temperature.innerHTML;
temperature.innerHTML = langstrings['pleaseallow'][language];
condition.style.visibility = "hidden";
document.getElementById("city").style.background = "url('spinning_wheel150.gif') no-repeat center bottom";
timeoutCity = setTimeout('cityTimeout()', 10000);
if (navigator.geolocation) { // Geolocation is supported
browserSupportsGeolocation = true;
timestamp("Geolocation is supported. Asking for location...");
navigator.geolocation.getCurrentPosition(
function(pos) { startGeoStuff(pos); }, // locating was successful
function() { noGeolocation(browserSupportsGeolocation); } // … was not successful
);
} else { // Geolocation is not supported
browserSupportsGeolocation = false;
noGeolocation(browserSupportsGeolocation);
}
}
function startGeoStuff(position) { // translate coordinates to city name and display
var la = position.coords.latitude;
var lo = position.coords.longitude;
timestamp("Location found: "+la+", "+lo);
var latlng = new google.maps.LatLng(la, lo);
var geocoder = new google.maps.Geocoder();
timestamp("Calling Google Maps API Geocoder...");
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
timestamp("Processing geocoder results...");
var ac = results[0].address_components;
for (i=0; i<ac.length; i++) {
if (ac[i].types[0] == "locality") {
var city = ac[i].long_name;
timestamp("City found: "+city);
clearTimeout(timeoutCity);
cityname.innerHTML = city;
cityname.style.visibility = "visible";
temperature.style.visibility = "hidden";
temperature.innerHTML = tempInnerHTML;
document.getElementById("city").style.background = "none";
document.getElementById("weather").style.background = "url('spinning_wheel50.gif') no-repeat center bottom";
showWeather(la, lo, city);
}
}
timestamp("Finished.");
}
} else {
timestamp("Geocoder failed due to: " + status);
}
});
}
function showWeather(la, lo, city) {
timeoutWeather = setTimeout('weatherTimeout()', 3000);
timestamp("Calling Google Weather API...");
http_request = new XMLHttpRequest();
if (http_request != null) {
/* calling adapter php file */
var xmlfile = 'googleweather.php?la='+la+'&lo='+lo+'&lang='+language;
timestamp("XMLHttpRequest established: "+xmlfile);
http_request.open('GET', xmlfile, false);
http_request.send(null);
var x = http_request.responseXML.documentElement;
timestamp("Interpreting XML...");
try {
var error = x.getElementsByTagName("problem_cause")[0];
if (error != undefined) {
timestamp("No weather data found using coordinates. Trying city name instead...");
var xmlfile = 'googleweather.php?city='+city+'&lang='+language;
timestamp("XMLHttpRequest established: "+xmlfile);
http_request.open('GET', xmlfile, false);
http_request.send(null);
var x = http_request.responseXML.documentElement;
timestamp("Interpreting XML...");
}
} catch (e) { }
/* getting data out of xml file */
var current = x.getElementsByTagName("current_conditions")[0];
var celsius = current.getElementsByTagName("temp_c")[0].getAttribute("data");
var wetter = current.getElementsByTagName("condition")[0].getAttribute("data");
timestamp("Weather found.");
clearTimeout(timeoutWeather);
/* display temperature and condition */
temperature.innerHTML = celsius+' °C';
condition.innerHTML = wetter;
temperature.style.visibility = "visible";
condition.style.visibility = "visible";
document.getElementById("weather").style.background = "none"; // hide progress indicator
} else {
timestamp("HTTP Request failed.");
clearTimeout(timeoutWeather);
weatherTimeout();
}
}
function noGeolocation(browserSupport) { // Error handling if no location is found
if (browserSupport == true) {
timestamp("Geolocation service failed.");
} else {
timestamp("Browser doesn't support geolocation.");
}
clearTimeout(timeoutCity);
cityTimeout();
}
function timestamp(string) { // output on console
d = new Date();
t = d.getTime() - time;
console.log(t+"ms: "+string);
}
function cityTimeout() { // City is not found
temperature.innerHTML = langstrings['cityTimeout'][language];
document.getElementById("city").style.background = "none";
timestamp("cityTimeout");
}
function weatherTimeout() { // Weather data is not found
temperature.innerHTML = langstrings['weatherTimeout'][language];
document.getElementById("weather").style.background = "none";
temperature.style.visibility = "visible";
timestamp("weatherTimeout");
}
function initLanguageStrings() {
langstrings = new Array();
langstrings['pleaseallow'] = new Array();
langstrings['pleaseallow']['en'] = "Please accept the geolocation request, if asked.";
langstrings['pleaseallow']['de'] = "Bitte erlauben Sie die Geolokalisierung, wenn danach gefragt wird.";
langstrings['cityTimeout'] = new Array();
langstrings['cityTimeout']['en'] = "Couldn't find your location.";
langstrings['cityTimeout']['de'] = "Ihr Aufenthaltsort konnte leider nicht festgestellt werden.";
langstrings['weatherTimeout'] = new Array();
langstrings['weatherTimeout']['en'] = "No weather data available.";
langstrings['weatherTimeout']['de'] = "Keine Wetterdaten gefunden.";
}