Web/index.html

202 lines
6.5 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>DCSW Server Map</title>
<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>
<link href="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
#info {
display: block;
position: relative;
margin: 0px auto;
width: 50%;
padding: 10px;
border: none;
border-radius: 3px;
font-size: 12px;
text-align: center;
color: #222;
background: #fff;
}
</style>
</head>
<body>
<div id="map"></div>
<pre id="info"></pre>
<script>
// Variables
const api_key = "rYNCGwdgATyHCjyDil7t";
const serverURL = "http://homeplate.close-air.support:8888/";
var latest = [];
var oldest= [];
var Units;
// Deploy stuff
var map = new maplibregl.Map({
center: [42, 42],
container: "map",
style: "https://api.maptiler.com/maps/topo-v2/style.json?key="+api_key,
zoom: 7
});
// Update, you know...the things.
map.on("load", () => {
window.setInterval( () => {
// Step 1 -- Update contents
fetchContent();
// Step 2 -- Add all the units added
units_added = unitsAdded();
if (units_added.length != 0) {
// units_added
// addElements(units_added);
for (i in units_added) {
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(
"/symbols/"+UnitCoalition+"/"+UnitType+".png",
(error, image) => {
if (error) throw error;
map.addImage(UnitImage, image);
map.addSource(UnitSource, { "type": 'geojson', 'data': { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [UnitLong, UnitLati] } }] } } );
map.addLayer({ 'id': UnitPoint, 'type': 'symbol', 'source': UnitSource, 'layout': { 'icon-image': UnitImage, 'icon-size': 0.25 } }); // map.addLayer()
}
); // map.loadImage()
};
};
// 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";
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;
const json = {
type: 'Feature',
geometry: {
'type': 'Point',
'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) {
//if (Lat < -80) return 'Too far South' ; if (Lat > 84) return 'Too far North' ;
var c = 1 + Math.floor ((Long+180)/6);
var e = c*6 - 183 ;
var k = Lat*Math.PI/180;
var l = Long*Math.PI/180;
var m = e*Math.PI/180;
var n = Math.cos (k);
var o = 0.006739496819936062*Math.pow (n,2);
var p = 40680631590769/(6356752.314*Math.sqrt(1 + o));
var q = Math.tan (k);
var r = q*q;
var s = (r*r*r) - Math.pow (q,6);
var t = l - m;
var u = 1.0 - r + o;
var v = 5.0 - r + 9*o + 4.0*(o*o);
var w = 5.0 - 18.0*r + (r*r) + 14.0*o - 58.0*r*o;
var x = 61.0 - 58.0*r + (r*r) + 270.0*o - 330.0*r*o;
var y = 61.0 - 479.0*r + 179.0*(r*r) - (r*r*r);
var z = 1385.0 - 3111.0*r + 543.0*(r*r) - (r*r*r);
var aa = p*n*t + (p/6.0*Math.pow (n,3)*u*Math.pow (t,3)) + (p/120.0*Math.pow (n,5)*w*Math.pow (t,5)) + (p/5040.0*Math.pow (n,7)*y*Math.pow (t,7));
var ab = 6367449.14570093*(k - (0.00251882794504*Math.sin (2*k)) + (0.00000264354112*Math.sin (4*k)) - (0.00000000345262*Math.sin (6*k)) + (0.000000000004892*Math.sin (8*k))) + (q/2.0*p*Math.pow (n,2)*Math.pow (t,2)) + (q/24.0*p*Math.pow (n,4)*v*Math.pow (t,4)) + (q/720.0*p*Math.pow (n,6)*x*Math.pow (t,6)) + (q/40320.0*p*Math.pow (n,8)*z*Math.pow (t,8));
aa = aa*0.9996 + 500000.0;
ab = ab*0.9996; if (ab < 0.0) ab += 10000000.0;
var ad = 'CDEFGHJKLMNPQRSTUVWXX'.charAt (Math.floor (Lat/8 + 10));
var ae = Math.floor (aa/100000);
var af = ['ABCDEFGH','JKLMNPQR','STUVWXYZ'][(c-1)%3].charAt (ae-1);
var ag = Math.floor (ab/100000)%20;
var ah = ['ABCDEFGHJKLMNPQRSTUV','FGHJKLMNPQRSTUVABCDE'][(c-1)%2].charAt (ag);
function pad (val) {if (val < 10) {val = '0000' + val} else if (val < 100) {val = '000' + val} else if (val < 1000) {val = '00' + val} else if (val < 10000) {val = '0' + val};return val};
aa = Math.floor (aa%100000); aa = pad (aa);
ab = Math.floor (ab%100000); ab = pad (ab);
return c + ad + ' ' + af + ah + ' ' + aa + ' ' + ab;
};
map.on('mousemove', function (e) {
var coordinate = e.lngLat.wrap();
document.getElementById('info').innerHTML = MGRSString(coordinate.lat, coordinate.lng);
});
</script>
</body>
</html>