Skip to content

Commit

Permalink
made it more flexible, it will still add new markers even after first…
Browse files Browse the repository at this point in the history
… load now.
  • Loading branch information
Kazim committed May 7, 2024
1 parent 36b0fbf commit 3e295a5
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions map.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@
<h1>Map starts from here</h1>
<div class="map" id="map"></div>
<script>

planes = {};
us_flight = 'https://opensky-network.org/api/states/all?lamin=30.038&lomin=-125.974&lamax=52.214&lomax=-68.748';


function plane_marker(plane) {
if (planes[plane[1]]) {
planes[plane[1]] = planes[plane[1]].setLatLng(new L.LatLng(plane[6], plane[5]));
} else {
planes[plane[1]] = L.marker([plane[6], plane[5]], {icon:greenIcon}).addTo(map);
}
}


map = L.map('map').setView([40.05264061210646, -102.4081931668813], 4);

L.tileLayer('https://api.maptiler.com/maps/openstreetmap/{z}/{x}/{y}.jpg?key=1oCmNBJXvSNb2J7xQsUM', {
maxZoom: 19,
attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>'
}).addTo(map);

greenIcon = L.icon({
iconUrl: 'flight_FILL0_wght400_GRAD0_opsz24.png',
iconSize: [24, 24],
iconAnchor: [12, 12],
popupAnchor: [-3, -76]
});
const us_flight = 'https://opensky-network.org/api/states/all?lamin=30.038&lomin=-125.974&lamax=52.214&lomax=-68.748';

var xml = new XMLHttpRequest();
xml.open('GET', us_flight, true);
xml.send();
Expand All @@ -38,14 +53,15 @@ <h1>Map starts from here</h1>
let response = JSON.parse(xml.response)['states'];
console.log('got the api')
for (i in response) {
planes[response[i][1]] = L.marker([response[i][6], response[i][5]], {icon:greenIcon}).addTo(map);
plane_marker(response[i], i)
if (i==10){break}
}
} else {
response = "error";
console.log('error in api')
}
}

setInterval(function() {
xml.open('GET', us_flight, true);
xml.send();
Expand All @@ -55,7 +71,7 @@ <h1>Map starts from here</h1>
let response = JSON.parse(xml.response)['states'];
console.log('got the api')
for (i in response) {
planes[response[i][1]] = planes[response[i][1]].setLatLng(new L.LatLng(response[i][6], response[i][5]));
plane_marker(response[i], i)
if (i==10){break}
}
} else {
Expand All @@ -64,6 +80,7 @@ <h1>Map starts from here</h1>
}
}
}, 12000)

</script>
</body>
</html>

0 comments on commit 3e295a5

Please sign in to comment.