-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
200 lines (155 loc) · 5.1 KB
/
app.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
/* app.js */
// DOM elements
const latSpan = document.querySelector('.lat') // Latitude
const lonSpan = document.querySelector('.lon') // Longitude
const altSpan = document.querySelector('.alt') // Altitude
const spdSpan = document.querySelector('.spd') // Orbital speed
const issInfo = document.querySelectorAll('span')
const mapBehaviorButtons = document.querySelectorAll('.map-behavior > * button')
const mapStyleButtons = document.querySelectorAll('.map-style > * button')
const unitButtons = document.querySelectorAll('.unit > * button')
// Global variables
const issData = 'https://api.wheretheiss.at/v1/satellites/25544'
const initialTiles = 'https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}'
const attribution = 'Map data © Google'
const iconOptions = {
iconUrl: './assets/icons/iss_dark.png',
iconSize: [100, 70],
iconAnchor: [50, 35],
}
let initialLoad = true
let isCentered = false
let unit = 'miles'
let alt, lat, lon, map, marker, spd, tiles
// Functions
const createMap = () => {
// Creates a map with tiles from Google and marker with ISS image
// Tiles
map = L.map('map').setView([0, 0], 1)
tiles = L.tileLayer(initialTiles, { attribution })
tiles.addTo(map)
// Marker
const issIcon = L.icon({ ...iconOptions })
marker = L.marker([0, 0], { icon: issIcon }).addTo(map)
}
const fetchIss = async () => {
// Fetches ISS data
try {
const response = await fetch(issData)
const data = await response.json()
return data
} catch (err) {
console.error(err)
}
}
const refreshInfo = async () => {
// Updates the global variables with new data
try {
const { altitude, latitude, longitude, velocity } = await fetchIss()
lat = latitude
lon = longitude
alt = altitude
spd = velocity
marker.setLatLng([lat, lon]) // Sets marker coordinates
checkMap()
refreshDom()
checkInitialLoad()
} catch (err) {
console.error(err)
issInfo.forEach((topic) => (topic.textContent = 'not available'))
}
}
const refreshDom = () => {
// Updates the DOM elements
// Altitude is given in KILOMETERS
// Velocity is given in KILOMETERS PER HOUR
// Conversion factors (to be multiplied by)
const KILOMETER_TO_MILE = 0.621371
const HOUR_TO_SECOND = 0.000277778
latSpan.textContent = `${lat.toFixed(2)}°`
lonSpan.textContent = `${lon.toFixed(2)}°`
// prettier-ignore
switch (unit) {
case 'miles':
default:
altSpan.textContent = (alt * KILOMETER_TO_MILE).toFixed(2) + ' miles'
spdSpan.textContent = (spd * KILOMETER_TO_MILE * HOUR_TO_SECOND).toFixed(2) + ' miles/s'
break
case 'kilometers':
altSpan.textContent = alt.toFixed(2) + ' km'
spdSpan.textContent = (spd * HOUR_TO_SECOND).toFixed(2) + ' km/s'
break
}
}
const checkMap = () => {
// Sets map view to always match the ISS location if 'Center ISS' is selected
if (!isCentered) return
map.setView([lat, lon])
}
const checkInitialLoad = () => {
// Sets map view to match the ISS location when the app starts
if (!initialLoad) return
map.setView([lat, lon], 3)
initialLoad = false
}
const changeMapBehavior = (e) => {
// Changes the behavior of the map, whether it is free to move or centered on the ISS position
const button = e.target
const { mapBehavior } = button.dataset
switch (mapBehavior) {
case 'free':
default:
isCentered = false
break
case 'centered':
map.setView([lat, lon])
isCentered = true
break
}
// Highlights the active button
mapBehaviorButtons.forEach((button) => button.classList.remove('active'))
button.classList.add('active')
}
const changeMapStyle = (e) => {
// Changes the map tiles and marker based in the selected style
const button = e.target
const { mapStyle } = button.dataset
const iconTheme = mapStyle === 'm' || mapStyle === 'p' ? 'dark' : 'light'
// Layer cleanup (map and marker)
tiles.remove()
marker.remove()
// New tiles and marker
const tileUrl = `https://mt1.google.com/vt/lyrs=${mapStyle}&x={x}&y={y}&z={z}`
const issIcon = L.icon({
...iconOptions,
iconUrl: `./assets/icons/iss_${iconTheme}.png`,
})
tiles = L.tileLayer(tileUrl, { attribution }).addTo(map)
marker = L.marker([lat, lon], { icon: issIcon }).addTo(map)
// Highlights the active button
mapStyleButtons.forEach((button) => button.classList.remove('active'))
button.classList.add('active')
}
const changeUnit = (e) => {
// Changes between miles and kilometers
const button = e.target
unit = button.dataset.unit
refreshDom()
// Highlights the active button
unitButtons.forEach((button) => button.classList.remove('active'))
button.classList.add('active')
}
const init = () => {
// Initiates the application
createMap()
setInterval(refreshInfo, 1000) // Updates data every second
}
// Event listeners
mapBehaviorButtons.forEach((button) =>
button.addEventListener('click', (e) => changeMapBehavior(e))
)
mapStyleButtons.forEach((button) =>
button.addEventListener('click', (e) => changeMapStyle(e))
)
unitButtons.forEach((button) => button.addEventListener('click', (e) => changeUnit(e)))
document.addEventListener('DOMContentLoaded', init)