Util.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Windows.Data.Xml.Dom;
  8. using Windows.Devices.Geolocation;
  9. using Windows.Services.Maps;
  10. using Windows.UI.Notifications;
  11. using Windows.UI.Popups;
  12. namespace YJMPD_UWP.Helpers
  13. {
  14. public class Util
  15. {
  16. public enum DialogType { YESNO, OKCANCEL }
  17. public enum RandomType { ALPHA, NUMERIC, ALPHANUMERIC }
  18. private static string AlphaSource = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  19. private static string NumericSource = "0123456789";
  20. public static double Now { get { return (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; } }
  21. public static string Random(int length, RandomType type)
  22. {
  23. string str = "";
  24. string source = "";
  25. switch(type)
  26. {
  27. case RandomType.ALPHA:
  28. source = AlphaSource;
  29. break;
  30. case RandomType.NUMERIC:
  31. source = NumericSource;
  32. break;
  33. default:
  34. case RandomType.ALPHANUMERIC:
  35. source = AlphaSource + NumericSource;
  36. break;
  37. }
  38. Random rand = new Random();
  39. for(int i = 0; i<length; i++)
  40. {
  41. str += source.ElementAt(rand.Next(0,source.Length-1));
  42. }
  43. return str;
  44. }
  45. public static string MillisecondsToTime(double millis)
  46. {
  47. DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  48. string timestr = time.AddMilliseconds(millis) + "";
  49. return timestr;
  50. }
  51. public static string Serialize<E>(E o)
  52. {
  53. return JsonConvert.SerializeObject(o);
  54. }
  55. public static E Deserialize<E>(string s)
  56. {
  57. return JsonConvert.DeserializeObject<E>(s);
  58. }
  59. //GPS
  60. public static async Task<Geopoint> FindLocation(string location, Geopoint reference)
  61. {
  62. MapLocationFinderResult result = await MapLocationFinder.FindLocationsAsync(location, reference);
  63. MapLocation from = result.Locations.FirstOrDefault();
  64. Geopoint p = from.Point;
  65. return p;
  66. }
  67. public static async Task<MapRoute> FindWalkingRoute(Geopoint from, Geopoint to)
  68. {
  69. MapRouteFinderResult routeResult = await MapRouteFinder.GetWalkingRouteAsync(from, to);
  70. MapRoute b = routeResult.Route;
  71. return b;
  72. }
  73. public static async Task<MapRoute> FindWalkingRoute(List<Geopoint> points)
  74. {
  75. MapRouteFinderResult routeResult = await MapRouteFinder.GetWalkingRouteFromWaypointsAsync(points);
  76. MapRoute b = routeResult.Route;
  77. return b;
  78. }
  79. public static async Task<MapRoute> FindWalkingRoute(string from, string to, Geopoint reference)
  80. {
  81. Geopoint f = await FindLocation(from, reference);
  82. Geopoint t = await FindLocation(to, reference);
  83. MapRoute m = await FindWalkingRoute(f, t);
  84. return m;
  85. }
  86. public static async Task<String> FindAddress(Geopoint p)
  87. {
  88. // Reverse geocode the specified geographic location.
  89. MapLocationFinderResult result =
  90. await MapLocationFinder.FindLocationsAtAsync(p);
  91. string returnstring = "";
  92. // If the query returns results, display the name of the town
  93. // contained in the address of the first result.
  94. if (result.Status == MapLocationFinderStatus.Success)
  95. {
  96. MapAddress address = result.Locations[0].Address;
  97. //returnstring = address.Street + " " + address.StreetNumber + ", " + address.Town;
  98. returnstring += (address.BuildingName == "" ? "" : address.BuildingName + ", ");
  99. returnstring += (address.Street == "" ? "" : address.Street + (address.StreetNumber == "" ? ", " : " " + address.StreetNumber + ", "));
  100. returnstring += address.Town;
  101. }
  102. return returnstring;
  103. }
  104. public static async Task<String> FindAddress(double latitude, double longitude)
  105. {
  106. Geopoint p = new Geopoint(new BasicGeoposition() { Latitude = latitude, Longitude = longitude });
  107. string address = await FindAddress(p);
  108. return address;
  109. }
  110. public static double Distance(BasicGeoposition pos1, BasicGeoposition pos2)
  111. {
  112. var R = 6371; // Radius of the earth in km
  113. var dLat = toRadian((pos2.Latitude - pos1.Latitude)); // deg2rad below
  114. var dLon = toRadian(pos2.Longitude - pos1.Longitude);
  115. var a =
  116. Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
  117. Math.Cos(toRadian(pos1.Latitude)) * Math.Cos(toRadian(pos2.Latitude)) *
  118. Math.Sin(dLon / 2) * Math.Sin(dLon / 2)
  119. ;
  120. var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
  121. var d = R * c; // Distance in m
  122. return d;
  123. }
  124. public static double DegreeBearing(BasicGeoposition pos1, BasicGeoposition pos2)
  125. {
  126. var R = 6371000.0;
  127. var φ1 = toRadian(pos1.Latitude);
  128. var φ2 = toRadian(pos2.Latitude);
  129. var Δφ = toRadian(pos2.Latitude - pos1.Latitude);
  130. var Δλ = toRadian(pos2.Longitude - pos2.Longitude);
  131. var a = Math.Sin(Δφ / 2.0) * Math.Sin(Δφ / 2.0) +
  132. Math.Cos(φ1) * Math.Cos(φ2) *
  133. Math.Sin(Δλ / 2.0)* Math.Sin(Δλ / 2.0);
  134. var c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));
  135. var d = R * c;
  136. return d %360;
  137. }
  138. private static double toRadian(double val)
  139. {
  140. return (Math.PI / 180) * val;
  141. }
  142. public static void ShowToastNotification(string title, string text)
  143. {
  144. ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
  145. XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
  146. XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
  147. toastTextElements[0].AppendChild(toastXml.CreateTextNode(title));
  148. toastTextElements[1].AppendChild(toastXml.CreateTextNode(text));
  149. IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
  150. XmlElement audio = toastXml.CreateElement("audio");
  151. audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
  152. toastNode.AppendChild(audio);
  153. ToastNotification toast = new ToastNotification(toastXml);
  154. ToastNotificationManager.CreateToastNotifier().Show(toast);
  155. }
  156. public static async Task<bool> ShowConfirmDialog(string title, string content, DialogType type)
  157. {
  158. MessageDialog dlg = new MessageDialog(content, title);
  159. if (type == DialogType.YESNO)
  160. {
  161. dlg.Commands.Add(new UICommand("Yes") { Id = 0 });
  162. dlg.Commands.Add(new UICommand("No") { Id = 1 });
  163. }
  164. else if (type == DialogType.OKCANCEL)
  165. {
  166. dlg.Commands.Add(new UICommand("Ok") { Id = 0 });
  167. dlg.Commands.Add(new UICommand("Cancel") { Id = 1 });
  168. }
  169. dlg.DefaultCommandIndex = 0;
  170. dlg.CancelCommandIndex = 1;
  171. var result = await dlg.ShowAsync();
  172. if ((int)result.Id == 0)
  173. return true;
  174. else
  175. return false;
  176. }
  177. }
  178. }