-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJsWeather.js
More file actions
41 lines (34 loc) · 1.25 KB
/
JsWeather.js
File metadata and controls
41 lines (34 loc) · 1.25 KB
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
function locationSuccess(pos) {
var coordinates = pos.coords;
fetchWeather(coordinates.latitude, coordinates.longitude);
}
function locationError(err) {
}
function fetchWeather(latitude, longitude) {
var response;
var req = new XMLHttpRequest();
var url = "http://api.openweathermap.org/data/2.5/weather?" + "lat=" + latitude + "&lon=" + longitude + "&cnt=1";
req.open('GET', url, true);
req.onload = function(e) {
if (req.readyState == 4) {
if (req.status == 200) {
response = JSON.parse(req.responseText);
var temperature;
if (response) {
var temp = response.main.temp;
var unit = " C";
if (response.sys.country === "US") {
temperature = Math.round(((temp - 273.15) * 1.8) + 32);
unit = " F";
}
else {
temperature = Math.round(temp - 273.15);
}
Talk2Watch.sendSms(temperature + unit, "Weather in " + response.name);
}
}
}
};
req.send(null);
}
window.navigator.geolocation.getCurrentPosition(locationSuccess,locationError);