index.html 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Honestly stolen from BlackSharkDen</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. var map = new maplibregl.Map({
  32. container: 'map',
  33. style: 'https://api.maptiler.com/maps/topo-v2/style.json?key=<replace-me>',
  34. center: [42, 42],
  35. zoom: 7
  36. });
  37. map.on('load', function () {
  38. });
  39. function MGRSString (Lat, Long) {
  40. //if (Lat < -80) return 'Too far South' ; if (Lat > 84) return 'Too far North' ;
  41. var c = 1 + Math.floor ((Long+180)/6);
  42. var e = c*6 - 183 ;
  43. var k = Lat*Math.PI/180;
  44. var l = Long*Math.PI/180;
  45. var m = e*Math.PI/180;
  46. var n = Math.cos (k);
  47. var o = 0.006739496819936062*Math.pow (n,2);
  48. var p = 40680631590769/(6356752.314*Math.sqrt(1 + o));
  49. var q = Math.tan (k);
  50. var r = q*q;
  51. var s = (r*r*r) - Math.pow (q,6);
  52. var t = l - m;
  53. var u = 1.0 - r + o;
  54. var v = 5.0 - r + 9*o + 4.0*(o*o);
  55. var w = 5.0 - 18.0*r + (r*r) + 14.0*o - 58.0*r*o;
  56. var x = 61.0 - 58.0*r + (r*r) + 270.0*o - 330.0*r*o;
  57. var y = 61.0 - 479.0*r + 179.0*(r*r) - (r*r*r);
  58. var z = 1385.0 - 3111.0*r + 543.0*(r*r) - (r*r*r);
  59. 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));
  60. 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));
  61. aa = aa*0.9996 + 500000.0;
  62. ab = ab*0.9996; if (ab < 0.0) ab += 10000000.0;
  63. var ad = 'CDEFGHJKLMNPQRSTUVWXX'.charAt (Math.floor (Lat/8 + 10));
  64. var ae = Math.floor (aa/100000);
  65. var af = ['ABCDEFGH','JKLMNPQR','STUVWXYZ'][(c-1)%3].charAt (ae-1);
  66. var ag = Math.floor (ab/100000)%20;
  67. var ah = ['ABCDEFGHJKLMNPQRSTUV','FGHJKLMNPQRSTUVABCDE'][(c-1)%2].charAt (ag);
  68. 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};
  69. aa = Math.floor (aa%100000); aa = pad (aa);
  70. ab = Math.floor (ab%100000); ab = pad (ab);
  71. return c + ad + ' ' + af + ah + ' ' + aa + ' ' + ab;
  72. };
  73. map.on('mousemove', function (e) {
  74. var coordinate = e.lngLat.wrap();
  75. document.getElementById('info').innerHTML = MGRSString(coordinate.lat, coordinate.lng);
  76. });
  77. </script>
  78. </body>
  79. </html>