Util.cs 9.8 KB

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