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