CoordinateConverter.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CoordinateConverter
  5. {
  6. // 将经纬度转换为Unity坐标
  7. public static Vector3 GeoToUnity(double longitude, double latitude)
  8. {
  9. float x = (float)(96151.617 * longitude - 10938527.4823);
  10. float z = (float)(111468.3949 * latitude - 3339986.2697);
  11. return new Vector3(x, 0, z);
  12. }
  13. // 将Unity坐标转换为经纬度
  14. public static (double Longitude, double Latitude) UnityToGeo(Vector3 unityCoord)
  15. {
  16. double longitude = (unityCoord.x + 10938527.4823)/ 96151.617;
  17. double latitude = (unityCoord.z + 3339986.2697)/ 111468.3949;
  18. // 这里我们忽略了截距,因为我们假设Unity坐标的原点对应于经纬度的截距
  19. // 如果需要考虑截距,可以在这里添加相应的计算
  20. return (longitude, latitude);
  21. }
  22. public static Vector3 GeoToUGUI(double longitude, double latitude)
  23. {
  24. float x = (float)(355.53f * longitude - 40339.8622f);
  25. float y = (float)(409.37f * latitude - 12420.05f);
  26. return new Vector3(x, y, 0);
  27. }
  28. public static Vector3 GeoToUGUISmall(double longitude,double latitude)
  29. {
  30. float x = (float)(75.642f * longitude - 8607.658f);
  31. float y = (float)(87.990f * latitude - 2640.548f);
  32. return new Vector3(x, y, -0.400f);
  33. }
  34. public static (double Latitude, double Longitude) UGUISmallToGeo(Vector3 uiCoord)
  35. {
  36. double latitude = (uiCoord.x + 8607.658) / 75.642;
  37. double longitude = (uiCoord.y + 2640.548) / 87.990;
  38. // 这里我们忽略了截距,因为我们假设Unity坐标的原点对应于经纬度的截距
  39. // 如果需要考虑截距,可以在这里添加相应的计算
  40. return (latitude, longitude);
  41. }
  42. public static (double latitude, double longitude) UGUIToGeo(Vector3 ugui)
  43. {
  44. float x = ugui.x;
  45. float y = ugui.y;
  46. double latitude = (x + 40339.8622f) / 355.53f;
  47. double longitude = (y + 12420.05f) / 409.37f;
  48. return (latitude, longitude);
  49. }
  50. }