-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
63 lines (52 loc) · 1.56 KB
/
weather.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
var openweathermap = require('./openweathermapkey');
/**
* Gets weather data from the OpenWeatherMap API.
*
* @param pos: Position object with the current GPS coordinates.
*/
function getOpenWeatherData(pos) {
/* Construct the URL. */
var url = 'http://api.openweathermap.org/data/2.5/weather?' +
'lat=' +pos.coords.latitude +
'&lon=' + pos.coords.longitude +
'&units=imperial' +
'&appid=' + openweathermap.api_key;
var xhr = new XMLHttpRequest();
xhr.onload = function() {
/* On load, parse the temperature from the response and send it to the watch. */
/* responseText will contain a JSON object with data info. */
var json = JSON.parse(this.responseText);
/* Assemble dictionary to send. */
var dictionary = {
'Temperature': json.main.temp
};
/* Send the message to the watch. */
Pebble.sendAppMessage(dictionary, function(e) {
console.log('Weather info sent to Pebble successfully.');
}, function(e) {
console.log('Error sending weather info to Pebble.');
});
};
/* Send the request. */
xhr.open('GET', url);
xhr.send();
}
/**
* Logs an error if GPS is unavailable.
*/
function locationError(err) {
console.log('Error requesting location.');
}
/**
* Gets the weather data for the current location.
*/
function getWeather() {
console.log('Getting weather data for current location.');
/* Get the current position and, if successful, the weather data. */
navigator.geolocation.getCurrentPosition(
getOpenWeatherData,
locationError,
{timeout: 15000, maximumAge: 60000}
);
}
module.exports.getWeather = getWeather;