-
Notifications
You must be signed in to change notification settings - Fork 0
/
ISS Tracker.js
148 lines (125 loc) · 4.02 KB
/
ISS Tracker.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
/*
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function(ext) {
var EARTH_RADIUS = 6371000; //meters
//Radius in km to determine if the ISS is overhead
var OVERHEAD_DIST = 500
var locations = {};
var issData = null;
function getLocation(str, callback) {
if (locations[str]) {
callback(locations[str]);
return;
}
$.ajax({
type: "GET",
url: "http://nominatim.openstreetmap.org/search/",
dataType: "jsonp",
data: {
format: "json",
q: str
},
jsonp: "json_callback",
success: function(data) {
locations[str] = {};
locations[str].coords = [data[0].lon, data[0].lat];
locations[str].overhead = false;
callback(locations[str]);
},
error: function(jqxhr, textStatus, error) {
callback(null);
}
});
}
function updateISSLocation() {
$.ajax({
type: "GET",
dataType: "json",
//url: "http://api.open-notify.org/iss-now.json",
url: "https://api.wheretheiss.at/v1/satellites/25544",
success: function(data) {
issData = data;
},
error: function(jqxhr, textStatus, error) {
console.log("Error downloading ISS data");
}
});
}
function distanceBetween(lat, lon, unit) {
var lat1 = lat * (Math.PI/180);
var lat2 = issData.latitude * (Math.PI/180);
var d1 = (issData.latitude - lat) * (Math.PI/180);
var d2 = (issData.longitude - lon) * (Math.PI/180);
var a = Math.sin(d1/2) * Math.sin(d1/2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(d2/2) * Math.sin(d2/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var dist = EARTH_RADIUS * c;
if (unit === "miles") dist *= 0.00062137;
else dist /= 1000;
return +(Math.round(dist + "e+2") + "e-2");
}
ext.whenISSPasses = function(str) {
if (!issData) return;
getLocation(str, function(loc) {
var dist = distanceBetween(loc.coords[1], loc.coords[0], "kilometers");
if (dist <= OVERHEAD_DIST) loc.overhead = true;
else loc.overhead = false;
});
if (!locations[str])
return false;
return locations[str].overhead;
};
ext.distanceFrom = function(str, unit, callback) {
getLocation(str, function(loc) {
var dist = distanceBetween(loc.coords[1], loc.coords[0], unit);
if (dist <= OVERHEAD_DIST) loc.overhead = true;
else loc.overhead = false;
callback(dist);
return;
});
};
ext.getISSInfo = function(stat) {
if (!issData) return;
if (stat === "longitude" || stat === "latitude")
return issData[stat].toFixed(6).toString();
else
return issData[stat].toFixed(2).toString();
};
ext._getStatus = function() {
return { status:2, msg:'Ready' };
};
ext._shutdown = function() {
if (poller) {
clearInterval(poller);
poller = null;
}
};
var descriptor = {
blocks: [
['h', 'when ISS passes over %s', 'whenISSPasses', 'Boston, MA'],
['R', 'distance from %s in %m.measurements', 'distanceFrom', 'Boston, MA', 'kilometers'],
['r', 'current ISS %m.loc', 'getISSInfo', 'longitude']
],
menus: {
loc: ['longitude', 'latitude', 'altitude', 'velocity'],
measurements: ['kilometers', 'miles']
},
url: 'http://khanning.github.io/scratch-isstracker-extension'
};
ScratchExtensions.register('ISS Tracker', descriptor, ext);
updateISSLocation();
var poller = setInterval(updateISSLocation, 2000);
})({});