MapPage.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using Windows.Foundation;
  8. using Windows.Foundation.Collections;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows.UI.Xaml.Controls.Primitives;
  12. using Windows.UI.Xaml.Data;
  13. using Windows.UI.Xaml.Input;
  14. using Windows.UI.Xaml.Media;
  15. using Windows.UI.Xaml.Navigation;
  16. using Windows.Devices.Geolocation;
  17. using Windows.Services.Maps;
  18. using Windows.Storage.Streams;
  19. using Windows.UI;
  20. using Windows.UI.Core;
  21. using Windows.UI.Xaml.Automation;
  22. using Windows.UI.Xaml.Controls.Maps;
  23. using Windows.UI.Xaml.Shapes;
  24. using Breda_Tour.CustomControls;
  25. using System.Diagnostics;
  26. using Windows.UI.Xaml.Automation.Peers;
  27. using Breda_Tour.Data;
  28. using Breda_Tour.RouteSelectScreen;
  29. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  30. namespace Breda_Tour.MapScreen
  31. {
  32. /// <summary>
  33. /// An empty page that can be used on its own or navigated to within a Frame.
  34. /// </summary>
  35. public sealed partial class MapPage : Page
  36. {
  37. private MapIcon marker;
  38. private Geopoint point;
  39. private Gps gps;
  40. private Route route;
  41. public MapPage()
  42. {
  43. this.NavigationCacheMode = NavigationCacheMode.Enabled;
  44. marker = new MapIcon();
  45. marker.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Marker.png"));
  46. marker.NormalizedAnchorPoint = new Point(0.5, 0.5);
  47. gps = new Gps(this);
  48. gps.Start();
  49. this.InitializeComponent();
  50. }
  51. protected override void OnNavigatedTo(NavigationEventArgs e)
  52. {
  53. SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
  54. DefaultPivot.SetCheckedButton(DefaultPivotControl.Tab.Map);
  55. if (RouteExample.fromRouteExamp)
  56. {
  57. RouteExample.fromRouteExamp = false;
  58. route = e.Parameter as Route;
  59. Map.MapElements.Clear();
  60. gps.History.Clear();
  61. ShowLocaton(gps.Position.Coordinate.Point);
  62. ShowWaypoints(route);
  63. ShowRoute();
  64. }
  65. }
  66. public async void ShowLocaton(Geopoint point)
  67. {
  68. this.point = point;
  69. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  70. {
  71. Map.Center = point;
  72. if (!Map.MapElements.Contains(marker))
  73. {
  74. Map.MapElements.Add(marker);
  75. }
  76. marker.Location = point;
  77. });
  78. await Map.TrySetViewAsync(point, 17);
  79. }
  80. public async void ShowRoute()
  81. {
  82. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  83. {
  84. Map.Routes.Add(RouteExample.routeView);
  85. });
  86. //await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
  87. //{
  88. // List<Geopoint> geopoints = new List<Geopoint>();
  89. // foreach (Waypoint wayPoint in route.Waypoints)
  90. // {
  91. // geopoints.Add(wayPoint.Position);
  92. // }
  93. // MapRouteFinderResult finder = await MapRouteFinder.GetWalkingRouteFromWaypointsAsync(geopoints);
  94. // if (finder.Status == MapRouteFinderStatus.Success)
  95. // {
  96. // MapRouteView routeView = new MapRouteView(finder.Route);
  97. // routeView.RouteColor = Colors.Firebrick;
  98. // routeView.OutlineColor = Colors.Black;
  99. // Map.Routes.Add(routeView);
  100. // }
  101. //});
  102. }
  103. public async void ShowWaypoints(Route route)
  104. {
  105. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  106. {
  107. for (int x = 0; x < route.Waypoints.Count; x++)
  108. {
  109. Waypoint wayp = route.Waypoints.ElementAt(x);
  110. MapIcon wp = new MapIcon { Location = wayp.Position, Title = (x + 1).ToString() };
  111. Map.MapElements.Add(wp);
  112. }
  113. //foreach (var waypoint in route.WayPoints)
  114. //{
  115. // MapIcon wp = new MapIcon() {Location = waypoint.Position, Title = waypoint.number.ToString()};
  116. // Map.MapElements.Add(wp);
  117. //}
  118. });
  119. }
  120. private void Map_OnMapElementClick(MapControl sender, MapElementClickEventArgs args)
  121. {
  122. MapIcon Icon = args.MapElements.FirstOrDefault(x => x is MapIcon) as MapIcon;
  123. for (int x = 0; x < route.Waypoints.Count; x++)
  124. {
  125. if (Icon.Title != "")
  126. {
  127. if (x + 1 == int.Parse(Icon.Title))
  128. {
  129. MainPage.RootFrame.Navigate(typeof(WpDetailPage), route.Waypoints.ElementAt(x));
  130. }
  131. }
  132. }
  133. //foreach (var waypoint in route.WayPoints)
  134. //{
  135. // if (Icon.Title != "")
  136. // {
  137. // if (waypoint.number == int.Parse(Icon.Title))
  138. // {
  139. // MainPage.RootFrame.Navigate(typeof(WpDetailPage), waypoint);
  140. // }
  141. // }
  142. //}
  143. }
  144. public async void DrawWalkingPath(List<BasicGeoposition> positions)
  145. {
  146. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  147. {
  148. MapPolyline mapPolyline = new MapPolyline();
  149. mapPolyline.StrokeColor = Colors.Blue;
  150. mapPolyline.StrokeThickness = 3;
  151. mapPolyline.Path = new Geopath(positions);
  152. Map.MapElements.Add(mapPolyline);
  153. });
  154. }
  155. }
  156. }