Util.cs 8.3 KB

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