Geolocation.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace InABox.Avalonia
  7. {
  8. public class GPSLocation
  9. {
  10. // public delegate void LocationEvent(GPSLocation sender);
  11. // public event LocationEvent? OnLocationFound;
  12. // public delegate void LocationError(GPSLocation sender, Exception error);
  13. // public event LocationError? OnLocationError;
  14. public TimeSpan ScanDelay { get; set; }
  15. public double Latitude { get; private set; }
  16. public double Longitude { get; private set; }
  17. public String Address { get; private set; }
  18. // private bool bLocating = false;
  19. public DateTime TimeStamp { get; private set; }
  20. public GPSLocation() : base()
  21. {
  22. TimeStamp = DateTime.MinValue;
  23. ScanDelay = new TimeSpan(0, 0, 0);
  24. Address = "Searching for GPS";
  25. }
  26. public bool RecentlyLocated
  27. {
  28. get
  29. {
  30. return (DateTime.Now.Subtract(TimeStamp).Ticks < ScanDelay.Ticks);
  31. }
  32. }
  33. private double DistanceBetween(double sLatitude, double sLongitude, double eLatitude,
  34. double eLongitude)
  35. {
  36. var radiansOverDegrees = (Math.PI / 180.0);
  37. var sLatitudeRadians = sLatitude * radiansOverDegrees;
  38. var sLongitudeRadians = sLongitude * radiansOverDegrees;
  39. var eLatitudeRadians = eLatitude * radiansOverDegrees;
  40. var eLongitudeRadians = eLongitude * radiansOverDegrees;
  41. var dLongitude = eLongitudeRadians - sLongitudeRadians;
  42. var dLatitude = eLatitudeRadians - sLatitudeRadians;
  43. var result1 = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
  44. Math.Cos(sLatitudeRadians) * Math.Cos(eLatitudeRadians) *
  45. Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);
  46. // Using 3956 as the number of miles around the earth
  47. var result2 = 3956.0 * 2.0 *
  48. Math.Atan2(Math.Sqrt(result1), Math.Sqrt(1.0 - result1));
  49. return result2;
  50. }
  51. // public void GetLocation(bool skiprecentlylocated = false)
  52. // {
  53. // if (bLocating || RecentlyLocated)
  54. // {
  55. // if (!skiprecentlylocated)
  56. // return;
  57. // }
  58. // bLocating = true;
  59. // // Don't reset this on every refresh, otherwise the final UI will randomly get "Searching for GPS" as the address
  60. // //Address = "Searching for GPS";
  61. // bool bOK = MobileUtils.IsPermitted<Permissions.LocationWhenInUse>().Result;
  62. //
  63. // Task.Run(async () =>
  64. // {
  65. // try
  66. // {
  67. //
  68. // if (!bOK)
  69. // {
  70. // Latitude = 0.0F;
  71. // Longitude = 0.0F;
  72. // TimeStamp = DateTime.MinValue;
  73. // Address = "GPS Services Disabled";
  74. // Device.BeginInvokeOnMainThread(() =>
  75. // {
  76. // OnLocationError?.Invoke(this, new Exception("Please enable GPS Services to continue"));
  77. // });
  78. // bOK = false;
  79. // bLocating = false;
  80. // }
  81. // else
  82. // {
  83. // try
  84. // {
  85. // var request = new GeolocationRequest(GeolocationAccuracy.Best, new TimeSpan(0, 0, 20));
  86. // var location = await Geolocation.GetLocationAsync(request);
  87. // if (location != null)
  88. // {
  89. // //if (location.IsFromMockProvider)
  90. // //{
  91. // // Device.BeginInvokeOnMainThread(() =>
  92. // // {
  93. // // OnLocationError?.Invoke(this, new Exception("Mock GPS Location Detected!\nPlease correct and restart TimeBench."));
  94. // // });
  95. // //}
  96. // //else
  97. // {
  98. // Latitude = location.Latitude;
  99. // Longitude = location.Longitude;
  100. // TimeStamp = DateTime.Now;
  101. // String sErr = "";
  102. // Placemark address = null;
  103. // try
  104. // {
  105. // var addresses = await Geocoding.GetPlacemarksAsync(Latitude, Longitude);
  106. // double maxdist = double.MaxValue;
  107. // foreach (var cur in addresses.Where(x => !String.IsNullOrEmpty(x.Thoroughfare)))
  108. // {
  109. // var delta = Location.CalculateDistance(location, cur.Location, DistanceUnits.Kilometers);
  110. // if (delta < maxdist)
  111. // {
  112. // address = cur;
  113. // maxdist = delta;
  114. // }
  115. // }
  116. // }
  117. //
  118. // catch (Exception ee2)
  119. // {
  120. // sErr = ee2.Message;
  121. // //address = null;
  122. // }
  123. // if (address != null)
  124. // Address = String.Format("{0} {1} {2}", address.SubThoroughfare, address.Thoroughfare, address.Locality);
  125. // else
  126. // Address = String.Format("Lat: {0}, Lng: {1}", Latitude, Longitude);
  127. // if (location.IsFromMockProvider)
  128. // Address = "** " + Address;
  129. // if (!String.IsNullOrEmpty(sErr))
  130. // Address = String.Format("{0} (ERROR: {1})", Address, sErr);
  131. // Device.BeginInvokeOnMainThread(() =>
  132. // {
  133. // OnLocationFound?.Invoke(this);
  134. // });
  135. // bLocating = false;
  136. // }
  137. // }
  138. // else
  139. // {
  140. // Latitude = 0.00;
  141. // Longitude = 0.00;
  142. // TimeStamp = DateTime.MinValue;
  143. // bLocating = false;
  144. // Device.BeginInvokeOnMainThread(() =>
  145. // {
  146. // OnLocationError?.Invoke(this, new Exception("Unable to get GPS Location"));
  147. // });
  148. // }
  149. // }
  150. // catch (Exception e)
  151. // {
  152. // bLocating = false;
  153. // Device.BeginInvokeOnMainThread(() =>
  154. // {
  155. // OnLocationError?.Invoke(this, e);
  156. // });
  157. // }
  158. // }
  159. // }
  160. // catch (Exception e)
  161. // {
  162. //
  163. // }
  164. // bLocating = false;
  165. // });
  166. // }
  167. }
  168. }