RealWorldTerrainReverseGeocodingWindow.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* INFINITY CODE */
  2. /* https://infinity-code.com */
  3. using InfinityCode.RealWorldTerrain.Webservices;
  4. using InfinityCode.RealWorldTerrain.Webservices.Results;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace InfinityCode.RealWorldTerrain.Tools
  8. {
  9. public class RealWorldTerrainReverseGeocodingWindow: EditorWindow
  10. {
  11. private static RealWorldTerrainReverseGeocodingWindow wnd;
  12. private RealWorldTerrainMonoBase target;
  13. private double cursorLatitude;
  14. private double cursorLongitude;
  15. private double cursorAltitude;
  16. private float lat;
  17. private float lng;
  18. private bool hasCoordinates = false;
  19. private string languageCode = "en";
  20. private string formattedAddress;
  21. private string response;
  22. private Vector2 scrollPosition;
  23. private string key;
  24. private void OnDestroy()
  25. {
  26. EditorApplication.update -= OnUpdate;
  27. #if UNITY_2019_1_OR_NEWER
  28. SceneView.duringSceneGui -= OnSceneGUI;
  29. #else
  30. SceneView.onSceneGUIDelegate -= OnSceneGUI;
  31. #endif
  32. wnd = null;
  33. }
  34. private void OnEnable()
  35. {
  36. EditorApplication.update += OnUpdate;
  37. #if UNITY_2019_1_OR_NEWER
  38. SceneView.duringSceneGui += OnSceneGUI;
  39. #else
  40. SceneView.onSceneGUIDelegate += OnSceneGUI;
  41. #endif
  42. }
  43. private void OnGUI()
  44. {
  45. target = EditorGUILayout.ObjectField("Container", target, typeof(RealWorldTerrainContainer), true) as RealWorldTerrainContainer;
  46. lat = EditorGUILayout.FloatField("Latitude", lat);
  47. lng = EditorGUILayout.FloatField("Longitude", lng);
  48. key = EditorGUILayout.TextField("Google API key", key);
  49. EditorGUILayout.BeginHorizontal();
  50. languageCode = EditorGUILayout.TextField("Language Code", languageCode);
  51. RealWorldTerrainWindowUI.DrawHelpButton("List of Languages", "https://developers.google.com/maps/faq?hl=en#languagesupport");
  52. EditorGUILayout.EndHorizontal();
  53. if (GUILayout.Button("What's here?"))
  54. {
  55. RealWorldTerrainGoogleGeocoding.Find(
  56. new RealWorldTerrainGoogleGeocoding.ReverseGeocodingParams(lng, lat)
  57. {
  58. language = languageCode,
  59. key = key
  60. }
  61. ).OnComplete += OnRequestComplete;
  62. }
  63. if (hasCoordinates)
  64. {
  65. EditorGUILayout.LabelField("Cursor Coordinates:");
  66. EditorGUILayout.LabelField("Latitude: ", cursorLatitude.ToString());
  67. EditorGUILayout.LabelField("Longitude: ", cursorLongitude.ToString());
  68. EditorGUILayout.LabelField("Altitude: ", cursorAltitude.ToString("F2") + " meters");
  69. EditorGUILayout.LabelField("Use CTRL+SHIFT to insert the coordinates.");
  70. if (Event.current.control && Event.current.shift)
  71. {
  72. lat = (float)cursorLatitude;
  73. lng = (float)cursorLongitude;
  74. }
  75. }
  76. EditorGUILayout.Space();
  77. if (!string.IsNullOrEmpty(formattedAddress))
  78. {
  79. GUILayout.Label("Formatted Address: " + formattedAddress, EditorStyles.wordWrappedLabel);
  80. }
  81. if (!string.IsNullOrEmpty(response))
  82. {
  83. GUILayout.Label("Full Response: ");
  84. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  85. EditorGUILayout.TextArea(response);
  86. EditorGUILayout.EndScrollView();
  87. }
  88. }
  89. private void OnRequestComplete(string response)
  90. {
  91. Debug.Log(response);
  92. this.response = response;
  93. try
  94. {
  95. RealWorldTerrainGoogleGeocodingResult[] result = RealWorldTerrainGoogleGeocoding.GetResults(response);
  96. if (result.Length > 0)
  97. {
  98. formattedAddress = result[0].formatted_address;
  99. }
  100. }
  101. catch
  102. {
  103. }
  104. }
  105. private void OnSceneGUI(SceneView view)
  106. {
  107. if (target == null) return;
  108. Vector2 mp = Event.current.mousePosition;
  109. mp.y = view.camera.pixelHeight - mp.y;
  110. hasCoordinates = target.GetCoordinatesByScreenPosition(mp, out cursorLongitude, out cursorLatitude, out cursorAltitude, view.camera);
  111. }
  112. private void OnUpdate()
  113. {
  114. Repaint();
  115. }
  116. [MenuItem("Window/Infinity Code/Real World Terrain/Tools/Reverse Geocoder")]
  117. public static void OpenWindow()
  118. {
  119. OpenWindow(null);
  120. }
  121. public static void OpenWindow(RealWorldTerrainMonoBase target)
  122. {
  123. if (wnd != null) wnd.Close();
  124. wnd = GetWindow<RealWorldTerrainReverseGeocodingWindow>(true, "Reverse Geocoder");
  125. if (target == null) wnd.target = FindObjectOfType<RealWorldTerrainContainer>();
  126. else wnd.target = target;
  127. }
  128. }
  129. }