MapPage.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Breda_Tour.Data;
  27. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  28. namespace Breda_Tour.MapScreen
  29. {
  30. /// <summary>
  31. /// An empty page that can be used on its own or navigated to within a Frame.
  32. /// </summary>
  33. public sealed partial class MapPage : Page
  34. {
  35. private MapIcon marker;
  36. private Geopoint point;
  37. private Gps gps;
  38. private Route route;
  39. public MapPage()
  40. {
  41. this.NavigationCacheMode = NavigationCacheMode.Enabled;
  42. marker = new MapIcon();
  43. marker.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Marker.png"));
  44. marker.NormalizedAnchorPoint = new Point(0.5, 0.5);
  45. gps = new Gps(this);
  46. gps.Start();
  47. RouteDatabase routeDB = new RouteDatabase();
  48. routeDB.readRoutes();
  49. Debug.Write("Groote van de routes is:" + routeDB.Routes.Count);
  50. route = routeDB.Routes.ElementAt(0);
  51. this.InitializeComponent();
  52. Debug.Write("New Map generated");
  53. }
  54. protected override void OnNavigatedTo(NavigationEventArgs e)
  55. {
  56. SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
  57. DefaultPivot.SetCheckedButton(DefaultPivotControl.Tab.Map);
  58. }
  59. public async void ShowLocaton(Geopoint point)
  60. {
  61. this.point = point;
  62. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  63. {
  64. Map.Center = point;
  65. if (!Map.MapElements.Contains(marker))
  66. {
  67. Map.MapElements.Add(marker);
  68. }
  69. marker.Location = point;
  70. });
  71. await Map.TrySetViewAsync(point, 17);
  72. ShowWaypoints(route);
  73. ShowRoute(route);
  74. }
  75. public async void ShowRoute(Route route)
  76. {
  77. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
  78. {
  79. List<Geopoint> geopoints = new List<Geopoint>();
  80. foreach (Waypoint wayPoint in route.Waypoints)
  81. {
  82. geopoints.Add(wayPoint.Position);
  83. }
  84. MapRouteFinderResult finder = await MapRouteFinder.GetWalkingRouteFromWaypointsAsync(geopoints);
  85. if (finder.Status == MapRouteFinderStatus.Success)
  86. {
  87. MapRouteView routeView = new MapRouteView(finder.Route);
  88. routeView.RouteColor = Colors.Firebrick;
  89. routeView.OutlineColor = Colors.Black;
  90. Map.Routes.Add(routeView);
  91. }
  92. });
  93. }
  94. public async void ShowWaypoints(Route route)
  95. {
  96. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  97. {
  98. for (int x = 0; x < route.Waypoints.Count; x++)
  99. {
  100. Waypoint wayp = route.Waypoints.ElementAt(x);
  101. MapIcon wp = new MapIcon { Location = wayp.Position, Title = x + 1.ToString() };
  102. Map.MapElements.Add(wp);
  103. }
  104. //foreach (var waypoint in route.WayPoints)
  105. //{
  106. // MapIcon wp = new MapIcon() {Location = waypoint.Position, Title = waypoint.number.ToString()};
  107. // Map.MapElements.Add(wp);
  108. //}
  109. });
  110. }
  111. private void Map_OnMapElementClick(MapControl sender, MapElementClickEventArgs args)
  112. {
  113. MapIcon Icon = args.MapElements.FirstOrDefault(x => x is MapIcon) as MapIcon;
  114. for (int x = 0; x < route.Waypoints.Count; x++)
  115. {
  116. if (Icon.Title != "")
  117. {
  118. if (x + 1 == int.Parse(Icon.Title))
  119. {
  120. MainPage.RootFrame.Navigate(typeof (WpDetailPage), route.Waypoints.ElementAt(x));
  121. }
  122. }
  123. }
  124. //foreach (var waypoint in route.WayPoints)
  125. //{
  126. // if (Icon.Title != "")
  127. // {
  128. // if (waypoint.number == int.Parse(Icon.Title))
  129. // {
  130. // MainPage.RootFrame.Navigate(typeof(WpDetailPage), waypoint);
  131. // }
  132. // }
  133. //}
  134. }
  135. }
  136. }