html 将纬度和经度转换为网页上的x,y网格

llycmphe  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(203)

我正在尝试将不同人所在位置的纬度和经度值转换为网格状的2D显示,其中y=经度,x =纬度2D。如何使用html、css、java来实现这一点?我刚刚从另一个问题中发现,也许我可以使用ArcGis Api将这些值转换为X和Y。但如何将它们放置在屏幕上,以便它们具有正确的位置?
我有这个经纬度代码:

  1. const getLocation = document.getElementById("btn");
  2. getLocation.addEventListener("click", evt=> {
  3. if("geolocation" in navigator) {
  4. navigator.geolocation.getCurrentPosition(position=> {
  5. let latitude = position.coords.latitude;
  6. let longitude = position.coords.longitude;
  7. console.log(latitude,longitude);
  8. },error=>{
  9. console.log(error.code);
  10. });
  11. }else {
  12. console.log("not supported")
  13. }
  14. });
bqujaahr

bqujaahr1#

所以,你已经确定了你需要把纬度/经度转换成笛卡尔坐标,也就是x和y坐标。
我的解决方案是canvas api,你可以在上面画出你想要的东西。
所以,这里开始了...希望它能有所帮助!
第一个
编辑:link可能有助于将经度/纬度转换为笛卡尔坐标
下面是一个使用背景图像的版本:

  1. // make the canvas element and add it to the DOM
  2. let canvas = document.createElement("canvas");
  3. canvas.width = 360;
  4. canvas.height = 180;
  5. canvas.style.border = "1px solid black";
  6. canvas.style.backgroundColor = "white";
  7. document.body.appendChild(canvas);
  8. const ctx = canvas.getContext("2d");
  9. // set the colour of the canvas background
  10. canvas.style.backgroundColor = "#999";
  11. canvas.width = 360; // 360deg
  12. canvas.height = 180;
  13. // declare var to hold our chart/background
  14. let worldImg;
  15. let loadImg = new Promise(() => {
  16. //Load an image
  17. worldImg = new Image();
  18. worldImg.addEventListener("load", loadHandler, false);
  19. worldImg.src =
  20. "https://stuartcodes.000webhostapp.com/pexels-pixabay-41949.png";
  21. console.log(worldImg);
  22. });
  23. //The loadHandler is called when the image has loaded
  24. function loadHandler() {
  25. console.log("OK");
  26. // draw the image that we want as the background
  27. ctx.drawImage(worldImg, 0, 0);
  28. // then draw everything else
  29. // define your orgin lat'/long' = 0 at center for instance
  30. let oX = canvas.width / 2;
  31. let oY = canvas.height / 2;
  32. // let's plot the oX/oY axis
  33. //a. Set the line style options
  34. ctx.strokeStyle = "#fff";
  35. ctx.lineWidth = 0.5;
  36. //b. Draw the x axis line
  37. ctx.beginPath();
  38. ctx.moveTo(0, oY); // set start point of line
  39. ctx.lineTo(oX * 2, oY); // end point of line
  40. ctx.stroke(); // draw it
  41. //c. Draw the y axis line
  42. ctx.beginPath();
  43. ctx.moveTo(oX, 0); // set start point of line
  44. ctx.lineTo(oX, oY * 2); // end point of line
  45. ctx.stroke(); // draw it
  46. // plot your coords
  47. // I'm using the canvas api's rectangle method to
  48. // draw a rectangle of the point we want to plot
  49. // for convenience!
  50. // convenience vars for our plot point's width/height
  51. let ppW = 10,
  52. ppH = 10;
  53. // and then it's useful to to have the center points!
  54. let ppwCenter = ppW / 2,
  55. pphCenter = ppH / 2;
  56. //Set the line and fill style options
  57. ctx.strokeStyle = "black";
  58. ctx.lineWidth = 1;
  59. ctx.fillStyle = "rgba(128, 0, 0, 0.75)";
  60. //Draw the rectangle
  61. ctx.beginPath();
  62. ctx.rect(oX - ppwCenter, oY - pphCenter, ppW, ppH); // rect(x, y, width, height)
  63. ctx.stroke();
  64. ctx.fill();
  65. // plot another point in the middle of the top right quadrant
  66. //Set the line and fill style options
  67. ctx.strokeStyle = "black";
  68. ctx.lineWidth = 1;
  69. ctx.fillStyle = "rgba(0, 0, 255, 0.75)";
  70. // vars for your plot point coords, long/lat
  71. let x, y;
  72. // ****** Ploting With Decimal Degrees ****** \\
  73. //your plot points could be defined as oX +/- something
  74. // it's up to you to define the resolution of your graph
  75. x = oX * 0.5; // half of the quadrant's width
  76. y = oY * 0.5; // half of the quadrant's height
  77. //Draw the rectangle
  78. ctx.beginPath();
  79. ctx.rect(oX - ppwCenter + x, oY - pphCenter - y, 10, 10); // rect(x, y, width, height)
  80. ctx.stroke();
  81. ctx.fill();
  82. console.log(oX, x, y);
  83. // So if you have your long/lat coords in decimal degrees
  84. /* https://www.latlong.net/degrees-minutes-seconds-to-decimal-degrees
  85. Decimal Degrees = degrees + (minutes/60) + (seconds/3600)
  86. DD = d + (min/60) + (sec/3600)
  87. */
  88. // you could simply plot
  89. x = 30.6789; // longitude in decimal deg
  90. // y = -1 * number is a correction for the canvas
  91. // so that negative numbers fall south of the equator
  92. y = -1 * 15.23456; // latitude in decimal deg
  93. //Set the line and fill style options
  94. ctx.strokeStyle = "black";
  95. ctx.lineWidth = 1;
  96. ctx.fillStyle = "rgba(255, 0, 255, 0.75)";
  97. //Draw the rectangle
  98. ctx.beginPath();
  99. ctx.rect(oX - ppwCenter + x, oY - pphCenter + y, 10, 10); // rect(x, y, width, height)
  100. ctx.stroke();
  101. ctx.fill();
  102. }
展开查看全部

相关问题