MapPage.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.Devices.Geolocation.Geofencing;
  27. using Windows.UI.Xaml.Automation.Peers;
  28. using Breda_Tour.Data;
  29. using Breda_Tour.RouteSelectScreen;
  30. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  31. namespace Breda_Tour.MapScreen
  32. {
  33. /// <summary>
  34. /// An empty page that can be used on its own or navigated to within a Frame.
  35. /// </summary>
  36. public sealed partial class MapPage : Page
  37. {
  38. private MapIcon marker;
  39. private Geopoint point;
  40. private Gps gps;
  41. private Route route;
  42. public MapPage()
  43. {
  44. GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChange;
  45. this.NavigationCacheMode = NavigationCacheMode.Enabled;
  46. marker = new MapIcon();
  47. marker.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Marker.png"));
  48. marker.NormalizedAnchorPoint = new Point(0.5, 0.5);
  49. gps = new Gps(this);
  50. gps.Start();
  51. this.InitializeComponent();
  52. }
  53. protected override void OnNavigatedTo(NavigationEventArgs e)
  54. {
  55. SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
  56. DefaultPivot.SetCheckedButton(DefaultPivotControl.Tab.Map);
  57. if (RouteExample.fromRouteExamp)
  58. {
  59. RouteExample.fromRouteExamp = false;
  60. route = e.Parameter as Route;
  61. Map.MapElements.Clear();
  62. gps.History.Clear();
  63. gps.Refresh();
  64. ShowWaypoints(route);
  65. //ShowRoute();
  66. }
  67. }
  68. public async void ShowLocaton(Geopoint point)
  69. {
  70. this.point = point;
  71. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  72. {
  73. Map.Center = point;
  74. if (!Map.MapElements.Contains(marker))
  75. {
  76. Map.MapElements.Add(marker);
  77. }
  78. marker.Location = point;
  79. });
  80. await Map.TrySetViewAsync(point, 17);
  81. }
  82. public async void ShowRoute()
  83. {
  84. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  85. {
  86. Map.Routes.Add(RouteExample.routeView);
  87. });
  88. //await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
  89. //{
  90. // List<Geopoint> geopoints = new List<Geopoint>();
  91. // foreach (Waypoint wayPoint in route.Waypoints)
  92. // {
  93. // geopoints.Add(wayPoint.Position);
  94. // }
  95. // MapRouteFinderResult finder = await MapRouteFinder.GetWalkingRouteFromWaypointsAsync(geopoints);
  96. // if (finder.Status == MapRouteFinderStatus.Success)
  97. // {
  98. // MapRouteView routeView = new MapRouteView(finder.Route);
  99. // routeView.RouteColor = Colors.Firebrick;
  100. // routeView.OutlineColor = Colors.Black;
  101. // Map.Routes.Add(routeView);
  102. // }
  103. //});
  104. }
  105. public async void ShowWaypoints(Route route)
  106. {
  107. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  108. {
  109. GeofenceMonitor.Current.Geofences.Clear();
  110. for (int x = 0; x < route.Waypoints.Count; x++)
  111. {
  112. //Waypoints handeling
  113. Waypoint wayp = route.Waypoints.ElementAt(x);
  114. MapIcon wp = new MapIcon { Location = wayp.Position, Title = (x + 1).ToString() };
  115. Map.MapElements.Add(wp);
  116. //Geofencing handeling
  117. string name = $"Fence {(x + 1).ToString()}";
  118. GeofenceMonitor.Current.Geofences.Add(new Geofence(name,new Geocircle(wayp.Position.Position,10), MonitoredGeofenceStates.Entered, true,TimeSpan.FromSeconds(3)));
  119. Debug.Write("Geofences count: "+ GeofenceMonitor.Current.Geofences.Count);
  120. }
  121. //foreach (var waypoint in route.WayPoints)
  122. //{
  123. // MapIcon wp = new MapIcon() {Location = waypoint.Position, Title = waypoint.number.ToString()};
  124. // Map.MapElements.Add(wp);
  125. //}
  126. });
  127. }
  128. private void Map_OnMapElementClick(MapControl sender, MapElementClickEventArgs args)
  129. {
  130. MapIcon Icon = args.MapElements.FirstOrDefault(x => x is MapIcon) as MapIcon;
  131. for (int x = 0; x < route.Waypoints.Count; x++)
  132. {
  133. if (Icon.Title != "")
  134. {
  135. if (x + 1 == int.Parse(Icon.Title))
  136. {
  137. MainPage.RootFrame.Navigate(typeof(WpDetailPage), new Tuple < Waypoint, Route >(route.Waypoints.ElementAt(x), route));
  138. }
  139. }
  140. }
  141. //foreach (var waypoint in route.WayPoints)
  142. //{
  143. // if (Icon.Title != "")
  144. // {
  145. // if (waypoint.number == int.Parse(Icon.Title))
  146. // {
  147. // MainPage.RootFrame.Navigate(typeof(WpDetailPage), waypoint);
  148. // }
  149. // }
  150. //}
  151. }
  152. public async void DrawWalkingPath(List<BasicGeoposition> positions)
  153. {
  154. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  155. {
  156. MapPolyline mapPolyline = new MapPolyline();
  157. mapPolyline.StrokeColor = Colors.Blue;
  158. mapPolyline.StrokeThickness = 3;
  159. mapPolyline.Path = new Geopath(positions);
  160. Map.MapElements.Add(mapPolyline);
  161. });
  162. }
  163. private void OnGeofenceStateChange(GeofenceMonitor sender, object args)
  164. {
  165. Debug.Write("Kappa circle entered!!");
  166. }
  167. }
  168. }