-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
263 lines (210 loc) · 6.28 KB
/
index.html
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Live Flight Tracker</title>
<link rel="shortcut icon" type="image/png" href="/images/plane.png">
<!-- Open Layers -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/ol.css" type="text/css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js"></script>
<!-- Styling -->
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
}
/* MAP */
.map:-webkit-full-screen {
height: 100%;
margin: 0;
}
.map:fullscreen {
height: 100%;
}
/* CONTROL BUTTONS */
.ol-control button{
height: 3em;
width: 3em;
}
.ol-rotate {
top: 4em;
right: .5em;
}
.ol-setgeo {
top: 7em;
left: .5em;
}
.ol-refresh{
bottom: 2em;
right: 1em;
}
.ol-refresh button{
height: 4em;
width: 10em;
}
/* The alert message box */
#alert-box {
display: none;
position: absolute;
top: 25px;
right: 50px ;
width: max-content;
padding: 20px;
background-color: #f44336; /* Red */
color: white;
margin-bottom: 15px;
z-index: 1000;
}
/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {
.ol-control button{
height: 2.5em;
width: 2.5em;
}
.ol-refresh button{
width: 8em;
height: 2.5em;
}
.ol-setgeo{
top: 8em;
}
}
</style>
</head>
<body>
<div id="map" class="map" style="border: 1px solid black; width: 100%;height:100%;"></div>
<div id="alert-box">Warning</div>
<script>
// Initialize: View
const view = new ol.View({
center: [0, 0],
zoom: 2,
});
// Initialize: Geolocation
var geolocation = new ol.Geolocation({
trackingOptions: {
enableHighAccuracy: true,
},
projection: view.getProjection(),
tracking: true
});
// Controls
class RefreshMap extends ol.control.Control {
/**
* @param {Object} [opt_options] Control options.
*/
constructor(opt_options) {
const options = opt_options || {};
const button = document.createElement('button');
button.innerHTML = 'Refresh';
button.className = ''
const element = document.createElement('div');
element.className = 'ol-unselectable ol-control ol-refresh';
element.appendChild(button);
super({
element: element,
target: options.target,
});
button.addEventListener('click', this.updateMap.bind(this), false);
}
// Function: Update Map
updateMap() {
fetch('https://opensky-network.org/api/states/all'
//,{headers: new Headers({'Authorization': 'Basic '+btoa('username:password')})}
)
.then(function (response) {
if (response.ok) {
return response.json();
}else{
if(response.status == 429){
document.getElementById("alert-box").innerHTML = "Error 429: reached API call limit ‼";
}else if(response.status == 404){
document.getElementById("alert-box").innerHTML = "Error 404: API server Not Found ‼";
}else{
document.getElementById("alert-box").innerHTML = "Error : unknown API response ‼";
}
document.getElementById("alert-box").style.display = "block";
setTimeout(function(){ document.getElementById("alert-box").style.display = "none"; }, 3000);
}
})
.then(function (data) {
// Initialize features arrays
var features = new ol.source.Vector();
// Go through every states
for (var i = 0; i < data.states.length; ++i) {
// Create a feature in Lon,Lat of state
var point = new ol.geom.Point(ol.proj.fromLonLat([data.states[i][5], data.states[i][6]]));
// Create feature of plane in correct bearing
var plane_feature = new ol.Feature({
geometry: point,
name: "plane",
bearing: data.states[i][10],
});
// Add the plane feature to the features of vectorLayer
features.addFeature(plane_feature);
}
// Set plane features as vector source to vectorLayer
vectorLayer.setSource(features);
});
}
}
class SetGeolocation extends ol.control.Control {
/**
* @param {Object} [opt_options] Control options.
*/
constructor(opt_options) {
const options = opt_options || {};
const button = document.createElement('button');
button.innerHTML = 'G';
const element = document.createElement('div');
element.className = 'set-geolocation ol-unselectable ol-control ol-setgeo';
element.appendChild(button);
super({
element: element,
target: options.target,
});
button.addEventListener('click', this.findUserLocation.bind(this), false);
}
// Function: Find User Location
findUserLocation() {
map.setView(new ol.View({
center: geolocation.getPosition(),
zoom: 10
}));
}
}
// Initialize: Vector Layer
var vectorLayer = new ol.layer.Vector({
source: null,
style: function (feature, resolution) {
return new ol.style.Style({
image: new ol.style.Icon({
//scale: 1/Math.pow(resolution, 1/2),
scale: 0.04,
src: 'plane.png',
rotation: feature.get('bearing'),
}),
});
}
});
// Initialize: Map
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer,
],
target: 'map',
view: view,
controls: ol.control.defaults.defaults().extend([
new RefreshMap(),
new SetGeolocation(),
new ol.control.FullScreen()
])
});
</script>
</body>
</html>