-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
240 lines (209 loc) · 7.72 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
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
import 'ol/ol.css';
import Feature from 'ol/Feature';
import Map from 'ol/Map';
import Point from 'ol/geom/Point';
import Polygon from 'ol/geom/Polygon';
import Polyline from 'ol/format/Polyline';
import VectorSource from 'ol/source/Vector';
import View from 'ol/View';
import XYZ from 'ol/source/XYZ';
import { OSM } from 'ol/source'
import {
Circle as CircleStyle,
Fill,
Icon,
Stroke,
Style,
Text
} from 'ol/style';
import 'ol/ol.css';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
import { easeOut } from 'ol/easing';
import { fromLonLat } from 'ol/proj';
import { getVectorContext } from 'ol/render';
import { unByKey } from 'ol/Observable';
import { hasFlag } from 'country-flag-icons'
import { countries } from 'country-flag-icons'
import { isoCountries, degrees_to_radians } from './utils'
import images from "./images/*.png";
import { mapTilerAPI_key } from './config'
// import { } from './ol-ext.css'
// import { Transform } from 'ol-ext';
//Convert country to code and to Flag:
// console.log(isoCountries['israel']);
// const flagSrc = "http://purecatamphetamine.github.io/country-flag-icons/3x2/#isoCountry#.svg"
/****************************************/
/* Basic Map */
/****************************************/
// API key at https://www.maptiler.com/cloud/
var key = mapTilerAPI_key;
var attributions = '<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> ' +
'<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>';
const mapVectorSource = new VectorSource({
features: []
})
const mapVectorLayer = new VectorLayer({
source: mapVectorSource,
});
const map = new Map({
target: document.getElementById('map'),
view: new View({
// center: center,
center: [35.217018, 31.771959],
projection: 'EPSG:4326',
zoom: 5,
minZoom: 1,
maxZoom: 20,
}),
layers: [
new TileLayer({
// source: new OSM() //original map
source: key ? new XYZ({
attributions: attributions,
url: 'https://api.maptiler.com/maps/hybrid/{z}/{x}/{y}.jpg?key=' + key,
tileSize: 512,
}) : new OSM(),
}), mapVectorLayer],
});
map.on('singleclick', function (event) {
// getData(event.coordinate);
map.forEachFeatureAtPixel(event.pixel, function (feature, layer) {
console.log(feature);
// getData(feature.getProperties());
// getData(layer.getProperties());
}, {
hitTolerance: 5
});
});
// const polygonFeature = new Feature(new Polygon([[[32.3254, 34.2654], [32.625, 31.654], [33.654, 32.123]]]));
// polygonFeature._isId = "yes";
// mapVectorLayer.getSource().addFeature(polygonFeature);
/****************************************/
/* Israel Start Icon */
/****************************************/
const startMarker = new Feature({
geometry: new Point([35.217018, 31.771959]),
});
startMarker.setStyle(new Style({
image: new Icon({
// color: 'yellow',
crossOrigin: 'anonymous',
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
// src: flagSrc.replace("#isoCountry#", isoCountries['israel']),
src: images.israelIcon,
// src: getUnicodeFlagIcon('US'),
// src: '/images/israelIcon.png'
// src: 'https://openlayers.org/en/v4.6.5/examples/data/icon.png'
}),
text: new Text({
font: '15px Narkisim, bold',
text: 'Israel',
fill: new Fill({ color: '#f3f3f3' }),
stroke: new Stroke({
color: '#002986', width: 3
})
}),
}))
mapVectorSource.addFeature(startMarker);
/****************************************/
/* Flash Animation */
/****************************************/
var duration = 3000;
function flash(feature) {
var start = new Date().getTime();
var listenerKey = mapVectorLayer.on('postrender', animate);
function animate(event) {
var vectorContext = getVectorContext(event);
var frameState = event.frameState;
var flashGeom = feature.getGeometry().clone();
var elapsed = frameState.time - start;
var elapsedRatio = elapsed / duration;
// radius will be 5 at start and 30 at end.
var radius = easeOut(elapsedRatio) * 25 + 5;
var opacity = easeOut(1 - elapsedRatio);
var style = new Style({
image: new CircleStyle({
radius: radius,
stroke: new Stroke({
color: 'rgba(243, 243, 243, ' + opacity + ')',
width: 0.25 + opacity,
}),
}),
});
vectorContext.setStyle(style);
vectorContext.drawGeometry(flashGeom);
if (elapsed > duration) {
unByKey(listenerKey);
return;
}
// tell OpenLayers to continue postrender animation
map.render();
}
}
mapVectorSource.on('addfeature', function (e) {
flash(e.feature);
});
/****************************************/
/* Request for Airplanes */
/****************************************/
function requestForIsraelAirplanes() {
let a = new Date().getTime();
let searchA = Math.floor(a / 1000 + 900 + 330 * 60)
console.log(searchA);
console.log(a);
let requestURL = 'https://opensky-network.org/api/states/all?time=' + searchA;
let request = new XMLHttpRequest();
request.open('GET', requestURL, true);
request.responseType = 'json';
request.onload = function (e) {
if (request.readyState === 4) {
if (request.status === 200) {
// console.log(request.response)
request.response.states.forEach((el) => {
// if (el[2] === "Israel" && !mapVectorSource.getFeatureById(parseInt(el[0]))) {
if (!mapVectorSource.getFeatureById(el[0])) {
console.log(el);
debugger
let airplane = new Feature({
geometry: new Point([el[5], el[6]]),
_icao24: el[0],
_callsign: el[1],
// id: el[0]
})
airplane.setId(parseInt(el[0]));
console.log(airplane);
console.log(mapVectorSource.getFeaturesAtCoordinate([el[5], el[6]]));
// airplane._icao24 = el[0];
// airplane._callsign = el[1];
airplane.setStyle(new Style({
image: new Icon({
src: images.airplane,
rotation: degrees_to_radians(el[10]),
})
}))
// let lon =
// let lat = el[6]
mapVectorSource.addFeature(airplane)
debugger
}
else { //update coordinates:
if (mapVectorSource.getFeatureById(parseInt(el[0]))) {
console.log(`airplane: ${el[0]} updated`);
mapVectorSource.getFeatureById(parseInt(el[0])).getGeometry().setCoordinates([el[5], el[6]]);
}
}
})
} else {
console.error(request.statusText);
}
}
}.bind(this);
request.onerror = function (e) {
console.error(request.statusText);
};
request.send(null);
}
setInterval(requestForIsraelAirplanes, 5000);
// requestForIsraelAirplanes();