Update index.html into a working version completing the PoC arc

This commit is contained in:
Sacha Ligthert 2024-01-23 20:11:13 +00:00
parent 84d1a56cb2
commit ac13371e40

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>Honestly stolen from BlackSharkDen</title> <title>DCSW Server Map</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.js"></script> <script src="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.js"></script>
<link href="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.css" rel="stylesheet" /> <link href="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.css" rel="stylesheet" />
@ -28,59 +28,132 @@
<div id="map"></div> <div id="map"></div>
<pre id="info"></pre> <pre id="info"></pre>
<script> <script>
// const sleep = ms => new Promise(r => setTimeout(r, ms)); // Variables
var api_key = "rYNCGwdgATyHCjyDil7t"; const api_key = "rYNCGwdgATyHCjyDil7t";
var serverURL = "http://homeplate.close-air.support:8888/"; const serverURL = "http://homeplate.close-air.support:8888/";
var map = new maplibregl.Map({ var latest = [];
var oldest= [];
var Units;
// Deploy stuff
var map = new maplibregl.Map({
center: [42, 42], center: [42, 42],
container: "map", container: "map",
style: "https://api.maptiler.com/maps/topo-v2/style.json?key="+api_key, style: "https://api.maptiler.com/maps/topo-v2/style.json?key="+api_key,
zoom: 7 zoom: 7
}); });
map.on("load", () => { // Update, you know...the things.
map.on("load", () => {
window.setInterval( () => {
myData = fetchData(serverURL); // Step 1 -- Update contents
for (chip in myData) fetchContent();
{
//latest.push(myData[chip].UnitName); // Step 2 -- Add all the units added
let UnitImage = myData[chip].UnitName+"-img"; units_added = unitsAdded();
let UnitSource = myData[chip].UnitName+"-src"; if (units_added.length != 0) {
let UnitPoint = myData[chip].UnitName+"-point"; // units_added
let UnitLong = myData[chip].Longitude; // addElements(units_added);
let UnitLati = myData[chip].Latitude; for (i in units_added) {
let UnitCoalition = myData[chip].Coalition; unit = fetchUnit(units_added[i])
let UnitImage = unit.UnitName+"-img";
let UnitSource = unit.UnitName+"-src";
let UnitPoint = unit.UnitName+"-point";
let UnitLong = unit.Longitude;
let UnitLati = unit.Latitude;
let UnitCoalition = unit.Coalition;
let UnitType = unit.Type;
map.loadImage( map.loadImage(
"/symbols/"+UnitCoalition+"/plane.png", "/symbols/"+UnitCoalition+"/"+UnitType+".png",
(error, image) => { (error, image) => {
if (error) throw error; if (error) throw error;
map.addImage(UnitImage, image); map.addImage(UnitImage, image);
map.addSource(UnitSource, { "type": 'geojson', 'data': { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [UnitLong, UnitLati] } }] } } ); map.addSource(UnitSource, { "type": 'geojson', 'data': { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [UnitLong, UnitLati] } }] } } );
map.addLayer({ map.addLayer({ 'id': UnitPoint, 'type': 'symbol', 'source': UnitSource, 'layout': { 'icon-image': UnitImage, 'icon-size': 0.25 } }); // map.addLayer()
'id': UnitPoint,
'type': 'symbol',
'source': UnitSource,
'layout': {
'icon-image': UnitImage,
'icon-size': 0.25
}
}); // map.addLayer()
} }
); // map.loadImage() ); // map.loadImage()
};
};
};// End for() loop // Step 3 -- Remove all the units removed
units_rmved = unitsRemoved();
if (units_rmved != 0) {
for (i in units_rmved) {
unit = units_rmved[i];
let UnitImage = unit+"-img";
let UnitSource = unit+"-src";
let UnitPoint = unit+"-point";
}); // End map.on() if (map.getLayer(UnitPoint)) map.removeLayer(UnitPoint);
map.removeSource(UnitSource);
if (map.hasImage(UnitImage)) map.removeImage(UnitImage);
};
};
// Step 4 -- Update all the Units positions
for (i in Units) {
let UnitSource = Units[i].UnitName+"-src";
let UnitLong = Units[i].Longitude;
let UnitLati = Units[i].Latitude;
// function wait(ms){ const json = {
// var start = new Date().getTime(); type: 'Feature',
// var end = start; geometry: {
// while(end < start + ms) { 'type': 'Point',
// end = new Date().getTime(); 'coordinates': [UnitLong, UnitLati]
// }; }
// }; };
map.getSource(UnitSource).setData(json);
};
}, 5000);
});
// Functions
function fetchUnit(unitName) {
for (i in Units) {
if (unitName == Units[i].UnitName) {
return Units[i];
};
};
};
function fetchContent() {
oldest = latest;
Units = fetchData(serverURL);
latest = convertJSON(Units);
};
function convertJSON(jsonFile) {
var myArray = [];
for (index in jsonFile) { myArray.push(jsonFile[index].UnitName); };
return myArray;
};
function unitsRemoved() {
var myArray = [];
myArray = oldest.filter(x => !latest.includes(x));
return myArray;
};
function unitsAdded() {
var myArray = [];
myArray = latest.filter(x => !oldest.includes(x));
return myArray;
};
function fetchData(url)
{
var xhr = new XMLHttpRequest();
var retval
xhr.open("GET", url, false);
xhr.send();
return JSON.parse(xhr.responseText)
};
function MGRSString (Lat, Long) { function MGRSString (Lat, Long) {
//if (Lat < -80) return 'Too far South' ; if (Lat > 84) return 'Too far North' ; //if (Lat < -80) return 'Too far South' ; if (Lat > 84) return 'Too far North' ;
@ -123,17 +196,6 @@
document.getElementById('info').innerHTML = MGRSString(coordinate.lat, coordinate.lng); document.getElementById('info').innerHTML = MGRSString(coordinate.lat, coordinate.lng);
}); });
function fetchData(url)
{
var xhr = new XMLHttpRequest();
var retval
xhr.open("GET", url, false);
xhr.send();
return JSON.parse(xhr.responseText)
}
</script> </script>
</body> </body>
</html> </html>