-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (59 loc) · 1.54 KB
/
index.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
var co = require('co'),
get = require('./src/GetRequest'),
maps = require('./src/GoogleMaps'),
updateTimeInterval = 1000,
api = "https://api.wheretheiss.at/v1/satellites/25544",
map, issmarker, issinfo, timer;
document.addEventListener('DOMContentLoaded', function() {
co(getLocationJSON)
.then(initLocation)
.then(addEvents)
.then(startPolling)
.catch(function(err) {
console.log(err.stack);
clearInterval(timer);
})
});
function *getLocationJSON() {
return yield get(api, 'json');
}
function initLocation(iss) {
map = maps.Init(iss.latitude, iss.longitude);
issmarker = maps.ISSMarker(map, iss);
issinfo = maps.InfoWindow(map, iss);
issinfo.open(map, issmarker);
return iss;
}
function addEvents(iss) {
google.maps.event.addListener(issmarker, 'click', function() {
issinfo.open(map, issmarker);
});
return iss;
}
function startPolling(iss) {
timer = setInterval(function() {
co(getLocationJSON)
.then(updateLocation)
.catch(function(err) {
clearInterval(timer);
console.log(err.stack);
});
}, updateTimeInterval);
return iss;
}
function updateLocation(iss) {
var newContent = maps.FormatISSInfo(iss);
if(issinfo.getContent !== newContent) issinfo.setContent(newContent);
var center = new google.maps.LatLng(iss.latitude, iss.longitude);
issmarker.setPosition(center);
map.panTo(center);
};
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js', {
scope: './'
}).then(function(reg) {
console.log("Service worker registered")
}).catch(function(err) {
console.log(err);
});
}