index.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>DCSW Server Map</title>
  6. <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
  7. <script src="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.js"></script>
  8. <link href="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.css" rel="stylesheet" />
  9. <style>
  10. body { margin: 0; padding: 0; }
  11. #map { position: absolute; top: 0; bottom: 0; width: 100%; }
  12. #info {
  13. display: block;
  14. position: relative;
  15. margin: 0px auto;
  16. width: 50%;
  17. padding: 10px;
  18. border: none;
  19. border-radius: 3px;
  20. font-size: 12px;
  21. text-align: center;
  22. color: #222;
  23. background: #fff;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="map"></div>
  29. <pre id="info"></pre>
  30. <script>
  31. // Variables
  32. const api_key = "rYNCGwdgATyHCjyDil7t";
  33. const serverURL = "http://homeplate.close-air.support:8888/";
  34. var latest = [];
  35. var oldest= [];
  36. var Units;
  37. // Deploy stuff
  38. var map = new maplibregl.Map({
  39. center: [42, 42],
  40. container: "map",
  41. style: "https://api.maptiler.com/maps/topo-v2/style.json?key="+api_key,
  42. zoom: 7
  43. });
  44. // Update, you know...the things.
  45. map.on("load", () => {
  46. window.setInterval( () => {
  47. // Step 1 -- Update contents
  48. fetchContent();
  49. // Step 2 -- Add all the units added
  50. units_added = unitsAdded();
  51. if (units_added.length != 0) {
  52. // units_added
  53. // addElements(units_added);
  54. for (i in units_added) {
  55. unit = fetchUnit(units_added[i])
  56. let UnitImage = unit.UnitName+"-img";
  57. let UnitSource = unit.UnitName+"-src";
  58. let UnitPoint = unit.UnitName+"-point";
  59. let UnitLong = unit.Longitude;
  60. let UnitLati = unit.Latitude;
  61. let UnitCoalition = unit.Coalition;
  62. let UnitType = unit.Type;
  63. map.loadImage(
  64. "/symbols/"+UnitCoalition+"/"+UnitType+".png",
  65. (error, image) => {
  66. if (error) throw error;
  67. map.addImage(UnitImage, image);
  68. map.addSource(UnitSource, { "type": 'geojson', 'data': { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [UnitLong, UnitLati] } }] } } );
  69. map.addLayer({ 'id': UnitPoint, 'type': 'symbol', 'source': UnitSource, 'layout': { 'icon-image': UnitImage, 'icon-size': 0.25 } }); // map.addLayer()
  70. }
  71. ); // map.loadImage()
  72. };
  73. };
  74. // Step 3 -- Remove all the units removed
  75. units_rmved = unitsRemoved();
  76. if (units_rmved != 0) {
  77. for (i in units_rmved) {
  78. unit = units_rmved[i];
  79. let UnitImage = unit+"-img";
  80. let UnitSource = unit+"-src";
  81. let UnitPoint = unit+"-point";
  82. if (map.getLayer(UnitPoint)) map.removeLayer(UnitPoint);
  83. map.removeSource(UnitSource);
  84. if (map.hasImage(UnitImage)) map.removeImage(UnitImage);
  85. };
  86. };
  87. // Step 4 -- Update all the Units positions
  88. for (i in Units) {
  89. let UnitSource = Units[i].UnitName+"-src";
  90. let UnitLong = Units[i].Longitude;
  91. let UnitLati = Units[i].Latitude;
  92. const json = {
  93. type: 'Feature',
  94. geometry: {
  95. 'type': 'Point',
  96. 'coordinates': [UnitLong, UnitLati]
  97. }
  98. };
  99. map.getSource(UnitSource).setData(json);
  100. };
  101. }, 5000);
  102. });
  103. // Functions
  104. function fetchUnit(unitName) {
  105. for (i in Units) {
  106. if (unitName == Units[i].UnitName) {
  107. return Units[i];
  108. };
  109. };
  110. };
  111. function fetchContent() {
  112. oldest = latest;
  113. Units = fetchData(serverURL);
  114. latest = convertJSON(Units);
  115. };
  116. function convertJSON(jsonFile) {
  117. var myArray = [];
  118. for (index in jsonFile) { myArray.push(jsonFile[index].UnitName); };
  119. return myArray;
  120. };
  121. function unitsRemoved() {
  122. var myArray = [];
  123. myArray = oldest.filter(x => !latest.includes(x));
  124. return myArray;
  125. };
  126. function unitsAdded() {
  127. var myArray = [];
  128. myArray = latest.filter(x => !oldest.includes(x));
  129. return myArray;
  130. };
  131. function fetchData(url)
  132. {
  133. var xhr = new XMLHttpRequest();
  134. var retval
  135. xhr.open("GET", url, false);
  136. xhr.send();
  137. return JSON.parse(xhr.responseText)
  138. };
  139. function MGRSString (Lat, Long) {
  140. //if (Lat < -80) return 'Too far South' ; if (Lat > 84) return 'Too far North' ;
  141. var c = 1 + Math.floor ((Long+180)/6);
  142. var e = c*6 - 183 ;
  143. var k = Lat*Math.PI/180;
  144. var l = Long*Math.PI/180;
  145. var m = e*Math.PI/180;
  146. var n = Math.cos (k);
  147. var o = 0.006739496819936062*Math.pow (n,2);
  148. var p = 40680631590769/(6356752.314*Math.sqrt(1 + o));
  149. var q = Math.tan (k);
  150. var r = q*q;
  151. var s = (r*r*r) - Math.pow (q,6);
  152. var t = l - m;
  153. var u = 1.0 - r + o;
  154. var v = 5.0 - r + 9*o + 4.0*(o*o);
  155. var w = 5.0 - 18.0*r + (r*r) + 14.0*o - 58.0*r*o;
  156. var x = 61.0 - 58.0*r + (r*r) + 270.0*o - 330.0*r*o;
  157. var y = 61.0 - 479.0*r + 179.0*(r*r) - (r*r*r);
  158. var z = 1385.0 - 3111.0*r + 543.0*(r*r) - (r*r*r);
  159. 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));
  160. 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));
  161. aa = aa*0.9996 + 500000.0;
  162. ab = ab*0.9996; if (ab < 0.0) ab += 10000000.0;
  163. var ad = 'CDEFGHJKLMNPQRSTUVWXX'.charAt (Math.floor (Lat/8 + 10));
  164. var ae = Math.floor (aa/100000);
  165. var af = ['ABCDEFGH','JKLMNPQR','STUVWXYZ'][(c-1)%3].charAt (ae-1);
  166. var ag = Math.floor (ab/100000)%20;
  167. var ah = ['ABCDEFGHJKLMNPQRSTUV','FGHJKLMNPQRSTUVABCDE'][(c-1)%2].charAt (ag);
  168. 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};
  169. aa = Math.floor (aa%100000); aa = pad (aa);
  170. ab = Math.floor (ab%100000); ab = pad (ab);
  171. return c + ad + ' ' + af + ah + ' ' + aa + ' ' + ab;
  172. };
  173. map.on('mousemove', function (e) {
  174. var coordinate = e.lngLat.wrap();
  175. document.getElementById('info').innerHTML = MGRSString(coordinate.lat, coordinate.lng);
  176. });
  177. </script>
  178. </body>
  179. </html>