MapPage.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.Data_;
  25. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  26. namespace Breda_Tour.MapScreen
  27. {
  28. /// <summary>
  29. /// An empty page that can be used on its own or navigated to within a Frame.
  30. /// </summary>
  31. public sealed partial class MapPage : Page
  32. {
  33. private MapIcon marker;
  34. private Geopoint point;
  35. private Gps gps;
  36. private Route route;
  37. public MapPage()
  38. {
  39. marker = new MapIcon();
  40. marker.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Marker.png"));
  41. marker.NormalizedAnchorPoint = new Point(0.5, 0.5);
  42. gps = new Gps(this);
  43. gps.Start();
  44. route = new Route("TestRoute","Test Description");
  45. route.CreateTestWaypoints();
  46. this.InitializeComponent();
  47. }
  48. protected override void OnNavigatedTo(NavigationEventArgs e)
  49. {
  50. }
  51. public async void ShowLocaton(Geopoint point)
  52. {
  53. this.point = point;
  54. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  55. {
  56. Map.Center = point;
  57. if (!Map.MapElements.Contains(marker))
  58. {
  59. Map.MapElements.Add(marker);
  60. }
  61. marker.Location = point;
  62. });
  63. await Map.TrySetViewAsync(point, 15);
  64. ShowWaypoints(route);
  65. ShowRoute(route);
  66. }
  67. public async void ShowRoute(Route route)
  68. {
  69. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
  70. {
  71. List<Geopoint> geopoints = new List<Geopoint>();
  72. foreach(WayPoint wayPoint in route.WayPoints)
  73. {
  74. geopoints.Add(wayPoint.Position);
  75. }
  76. MapRouteFinderResult finder = await MapRouteFinder.GetWalkingRouteFromWaypointsAsync(geopoints);
  77. if (finder.Status == MapRouteFinderStatus.Success)
  78. {
  79. MapRouteView routeView = new MapRouteView(finder.Route);
  80. routeView.RouteColor = Colors.Firebrick;
  81. routeView.OutlineColor = Colors.Black;
  82. Map.Routes.Add(routeView);
  83. }
  84. });
  85. }
  86. public async void ShowWaypoints(Route route)
  87. {
  88. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  89. {
  90. foreach (var waypoint in route.WayPoints)
  91. {
  92. MapIcon wp = new MapIcon() {Location = waypoint.Position};
  93. Map.MapElements.Add(wp);
  94. }
  95. });
  96. }
  97. }
  98. }