Util.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Windows.Data.Xml.Dom;
  6. using Windows.Devices.Geolocation;
  7. using Windows.Services.Maps;
  8. using Windows.UI;
  9. using Windows.UI.Notifications;
  10. using Windows.UI.Popups;
  11. using Windows.UI.Xaml.Controls.Maps;
  12. namespace YJMPD_UWP.Helpers
  13. {
  14. class Util
  15. {
  16. public enum DialogType { YESNO, OKCANCEL }
  17. public static double Now { get { return (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; } }
  18. public static string MillisecondsToTime(double millis)
  19. {
  20. DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  21. string timestr = time.AddMilliseconds(millis) + "";
  22. return timestr;
  23. }
  24. public static async Task<Geopoint> FindLocation(string location, Geopoint reference)
  25. {
  26. MapLocationFinderResult result = await MapLocationFinder.FindLocationsAsync(location, reference);
  27. MapLocation from = result.Locations.FirstOrDefault();
  28. Geopoint p = from.Point;
  29. return p;
  30. }
  31. public static async Task<MapRoute> FindWalkingRoute(Geopoint from, Geopoint to)
  32. {
  33. MapRouteFinderResult routeResult = await MapRouteFinder.GetWalkingRouteAsync(from, to);
  34. MapRoute b = routeResult.Route;
  35. return b;
  36. }
  37. public static async Task<MapRoute> FindWalkingRoute(List<Geopoint> points)
  38. {
  39. MapRouteFinderResult routeResult = await MapRouteFinder.GetWalkingRouteFromWaypointsAsync(points);
  40. MapRoute b = routeResult.Route;
  41. return b;
  42. }
  43. public static async Task<MapRoute> FindWalkingRoute(string from, string to, Geopoint reference)
  44. {
  45. Geopoint f = await FindLocation(from, reference);
  46. Geopoint t = await FindLocation(to, reference);
  47. MapRoute m = await FindWalkingRoute(f, t);
  48. return m;
  49. }
  50. public static async Task<String> FindAddress(Geopoint p)
  51. {
  52. // Reverse geocode the specified geographic location.
  53. MapLocationFinderResult result =
  54. await MapLocationFinder.FindLocationsAtAsync(p);
  55. string returnstring = "";
  56. // If the query returns results, display the name of the town
  57. // contained in the address of the first result.
  58. if (result.Status == MapLocationFinderStatus.Success)
  59. {
  60. MapAddress address = result.Locations[0].Address;
  61. //returnstring = address.Street + " " + address.StreetNumber + ", " + address.Town;
  62. returnstring += (address.BuildingName == "" ? "" : address.BuildingName + ", ");
  63. returnstring += (address.Street == "" ? "" : address.Street + (address.StreetNumber == "" ? ", " : " " + address.StreetNumber + ", "));
  64. returnstring += address.Town;
  65. }
  66. return returnstring;
  67. }
  68. public static async Task<String> FindAddress(double latitude, double longitude)
  69. {
  70. Geopoint p = new Geopoint(new BasicGeoposition() { Latitude = latitude, Longitude = longitude });
  71. string address = await FindAddress(p);
  72. return address;
  73. }
  74. public static MapPolyline GetRouteLine(MapRoute m, Color color, int zindex, int thickness = 5)
  75. {
  76. var line = new MapPolyline
  77. {
  78. StrokeThickness = thickness,
  79. StrokeColor = color,
  80. StrokeDashed = false,
  81. ZIndex = zindex
  82. };
  83. if (m != null)
  84. line.Path = new Geopath(m.Path.Positions);
  85. return line;
  86. }
  87. public static MapPolyline GetRouteLine(List<BasicGeoposition> positions, Color color, int zindex, int thickness = 5)
  88. {
  89. var line = new MapPolyline
  90. {
  91. StrokeThickness = thickness,
  92. StrokeColor = color,
  93. StrokeDashed = false,
  94. ZIndex = zindex
  95. };
  96. line.Path = new Geopath(positions);
  97. return line;
  98. }
  99. public static MapPolyline GetRouteLine(BasicGeoposition p1, BasicGeoposition p2, Color color, int zindex, int thickness = 5)
  100. {
  101. var line = new MapPolyline
  102. {
  103. StrokeThickness = thickness,
  104. StrokeColor = color,
  105. StrokeDashed = false,
  106. ZIndex = zindex
  107. };
  108. List<BasicGeoposition> plist = new List<BasicGeoposition>();
  109. plist.Add(p1);
  110. plist.Add(p2);
  111. line.Path = new Geopath(plist);
  112. return line;
  113. }
  114. public static void ShowToastNotification(string title, string text)
  115. {
  116. ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
  117. XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
  118. XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
  119. toastTextElements[0].AppendChild(toastXml.CreateTextNode(title));
  120. toastTextElements[1].AppendChild(toastXml.CreateTextNode(text));
  121. IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
  122. XmlElement audio = toastXml.CreateElement("audio");
  123. audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
  124. toastNode.AppendChild(audio);
  125. ToastNotification toast = new ToastNotification(toastXml);
  126. ToastNotificationManager.CreateToastNotifier().Show(toast);
  127. }
  128. public static async Task<bool> ShowConfirmDialog(string title, string content, DialogType type)
  129. {
  130. MessageDialog dlg = new MessageDialog(content, title);
  131. if (type == DialogType.YESNO)
  132. {
  133. dlg.Commands.Add(new UICommand("Yes") { Id = 0 });
  134. dlg.Commands.Add(new UICommand("No") { Id = 1 });
  135. }
  136. else if (type == DialogType.OKCANCEL)
  137. {
  138. dlg.Commands.Add(new UICommand("Ok") { Id = 0 });
  139. dlg.Commands.Add(new UICommand("Cancel") { Id = 1 });
  140. }
  141. dlg.DefaultCommandIndex = 0;
  142. dlg.CancelCommandIndex = 1;
  143. var result = await dlg.ShowAsync();
  144. if ((int)result.Id == 0)
  145. return true;
  146. else
  147. return false;
  148. }
  149. public static string TranslatedManeuver(MapRouteManeuver maneuver, int distance)
  150. {
  151. string response = "";
  152. bool onstreet = false;
  153. bool meters = true;
  154. distance = (int)Math.Round(distance / 5.0) * 5;
  155. switch (maneuver.Kind)
  156. {
  157. default:
  158. response = "RouteSeeMap";
  159. meters = false;
  160. break;
  161. case MapRouteManeuverKind.End:
  162. response = "RouteEnd";
  163. break;
  164. case MapRouteManeuverKind.GoStraight:
  165. response = "RouteGoStraight";
  166. onstreet = true;
  167. break;
  168. case MapRouteManeuverKind.None:
  169. response = "RouteNone";
  170. meters = false;
  171. break;
  172. case MapRouteManeuverKind.Start:
  173. response = "RouteStart";
  174. meters = false;
  175. break;
  176. case MapRouteManeuverKind.TurnHardLeft:
  177. case MapRouteManeuverKind.TurnLeft:
  178. response = "RouteLeft";
  179. onstreet = true;
  180. break;
  181. case MapRouteManeuverKind.TurnHardRight:
  182. case MapRouteManeuverKind.TurnRight:
  183. response = "RouteRight";
  184. onstreet = true;
  185. break;
  186. case MapRouteManeuverKind.TrafficCircleLeft:
  187. response = "RouteTrafficCircleLeft";
  188. onstreet = true;
  189. break;
  190. case MapRouteManeuverKind.TrafficCircleRight:
  191. response = "RouteTrafficCircleRight";
  192. onstreet = true;
  193. break;
  194. case MapRouteManeuverKind.TurnKeepLeft:
  195. case MapRouteManeuverKind.TurnLightLeft:
  196. response = "RouteKeepLeft";
  197. break;
  198. case MapRouteManeuverKind.TurnKeepRight:
  199. case MapRouteManeuverKind.TurnLightRight:
  200. response = "RouteKeepRight";
  201. break;
  202. case MapRouteManeuverKind.UTurnLeft:
  203. case MapRouteManeuverKind.UTurnRight:
  204. response = "RouteUTurn";
  205. break;
  206. }
  207. if (maneuver.StreetName == "")
  208. onstreet = false;
  209. if (distance < 10)
  210. meters = false;
  211. if (onstreet)
  212. response += " " + "RouteOn" + " " + maneuver.StreetName;
  213. if (meters)
  214. response = "RouteIn" + " " + distance + "m" + " " + response.ToLower();
  215. return response;
  216. }
  217. }
  218. }