MapPage.xaml.cs 5.3 KB

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