-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
212 lines (177 loc) · 7.12 KB
/
main.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
207
208
209
210
211
212
//=============================================================================
// This file tracks location and path of International Space Station.
//*****************************************************************************
// Variable declaration
let isSatelliteTracked = true;
let refreshInterval = 1000;
let satellitePath = [];
// Setup
let issTrackingElem = document.getElementById("iss_tracking_button");
issTrackingElem.innerHTML = "Stop ISS tracking"
issTrackingElem.style.backgroundColor = "darkred";
// Initialize and setup Leaflet map
let map = L.map('map', {
zoom: 6, // Initial zoom level
zoomDelta: 1, // Zoom levels using zoom buttons
wheelPxPerZoomLevel: 200, // Zoom levels using mouse wheel zoom
fadeAnimation: false // Tile animation
});
// Add OpenStreetMap tiles to Leaflet map
let tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
minZoom: 3,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
subdomains: ['a','b','c']
})
.addTo(map);
// Initialize satellite marker
let satelliteMarker = L.marker([0, 0]).addTo(map);
// Get satellite information regularly
let interval = setInterval(getSatelliteInfo, refreshInterval);
//=============================================================================
// This function gets satellite information and plots its current location
// along with its path.
//-----------------------------------------------------------------------------
async function getSatelliteInfo()
//-----------------------------------------------------------------------------
{
// Query satellite info
const api_url = 'https://api.wheretheiss.at/v1/satellites/25544';
const response = await fetch(api_url);
try {
// Get satellite data
const data = await response.json();
const {
latitude, longitude, name, velocity, visibility, altitude
} = data;
// Store satellite coordinates
satellitePath.push(new L.LatLng(latitude, longitude));
// Plot satellite path
new L.Polyline(satellitePath, {
color: 'red',
weight: 1,
opacity: 1,
smoothFactor: 1
})
.addTo(map);
// Get geoCoding info
let info = getGeoCodingInfo(latitude, longitude);
try {
const value = await info;
setupSatelliteMarker(
name, latitude, longitude, altitude, velocity, visibility,
value.geoCodingInfo, value.country, value.state, value.region,
value.city, value.town, value.postcode
)
} catch (error) {
console.log(error);
}
// Set map view focused on satellite
map.setView([latitude, longitude], map.getZoom());
} catch (error) {
console.log(error);
}
}
//=============================================================================
// This function sets up the satellite tracking marker and satellite
// information as popup text.
//-----------------------------------------------------------------------------
function setupSatelliteMarker(
name, lat, lng, alt, vel, vis, geoCodingInfo, country, state, region,
city, town, postcode
)
//-----------------------------------------------------------------------------
{
// Position satellite marker
satelliteMarker.setLatLng([lat, lng]);
// Define default geo-coding popup text
let popupText = "Satellite: " + name.toUpperCase() +
"<br/> Coordinates: [" + lat.toFixed(1) + "°, " +
lng.toFixed(1) + "°]" +
"<br/> Altitude: " + alt.toFixed(2) + " km" +
"<br/> Velocity: " + vel.toFixed(2) + " kph" +
"<br/> Visibility: " + vis +
"<br/><br/> Geo-coding info: ";
// Update popup text when geo-coding info is available
if (geoCodingInfo) {
popupText += "Available" + "<br/> Country: " + country;
// Add state info if it is available
if ("undefined".localeCompare(state) != 0) {
popupText += "<br/> State: " + state;
}
// Add region info if it is available
if ("undefined".localeCompare(region) != 0) {
popupText += "<br/> Region: " + region;
}
// Add city info if it is available
if ("undefined".localeCompare(city) != 0) {
popupText += "<br/> City: " + city;
}
// Add town info if it is available
if ("undefined".localeCompare(town) != 0) {
popupText += "<br/> Town: " + town;
}
// Add postcode info if it is available
if ("undefined".localeCompare(postcode) != 0) {
popupText + "<br/> Postcode: " + postcode;
}
}
// Update popup text when geo-coding info is unavailable
else {
popupText += "Unavailable";
}
// Add popup text to satellite marker
satelliteMarker.bindPopup(popupText);
// Open popup text
satelliteMarker.openPopup();
}
//=============================================================================
// This function takes the latitude and longitude as the arguments and provides
// geo-coding information if it is available.
//-----------------------------------------------------------------------------
async function getGeoCodingInfo(lat, lng)
//-----------------------------------------------------------------------------
{
let geoCodingInfo, country, state, region, city, town, postcode;
const url = "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat="+lat+"&lon="+lng;
const response = await fetch(url);
try {
const data = await response.json();
if (data.error == "Unable to geocode") {
geoCodingInfo = false;
}
else {
geoCodingInfo = true;
country = data.address.country;
state = data.address.state
region = data.address.region;
city = data.address.city;
town = data.address.town;
postcode = data.address.postcode;
}
return {geoCodingInfo, country, state, region, city, town, postcode};
} catch (error) {
console.log(error);
}
}
//=============================================================================
// This function sets the satellite tracking status.
//-----------------------------------------------------------------------------
function setSatelliteTrackingStatus()
//-----------------------------------------------------------------------------
{
// Stop tracking
if (isSatelliteTracked) {
clearInterval(interval);
isSatelliteTracked = false;
issTrackingElem.textContent = "Resume ISS tracking";
issTrackingElem.style.backgroundColor = "green";
}
// Resume tracking
else if (!isSatelliteTracked) {
interval = setInterval(getSatelliteInfo, refreshInterval);
isSatelliteTracked = true;
issTrackingElem.textContent = "Stop ISS tracking";
issTrackingElem.style.backgroundColor = "darkred";
}
}