MapPage.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Windows.Foundation;
  5. using Windows.UI.Xaml.Controls;
  6. using Windows.UI.Xaml.Navigation;
  7. using Windows.Devices.Geolocation;
  8. using Windows.Storage.Streams;
  9. using Windows.UI;
  10. using Windows.UI.Core;
  11. using Windows.UI.Xaml.Controls.Maps;
  12. using Breda_Tour.CustomControls;
  13. using Windows.Devices.Geolocation.Geofencing;
  14. using Windows.UI.Xaml;
  15. using Breda_Tour.Data;
  16. using Breda_Tour.RouteSelectScreen;
  17. namespace Breda_Tour.MapScreen
  18. {
  19. public sealed partial class MapPage : Page
  20. {
  21. private MapIcon marker;
  22. private Gps gps;
  23. private Route route;
  24. public MapPage()
  25. {
  26. GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChange;
  27. this.NavigationCacheMode = NavigationCacheMode.Enabled;
  28. marker = new MapIcon();
  29. marker.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Marker.png"));
  30. marker.NormalizedAnchorPoint = new Point(0.5, 0.5);
  31. gps = new Gps(this);
  32. gps.Start();
  33. this.InitializeComponent();
  34. }
  35. protected override void OnNavigatedTo(NavigationEventArgs e)
  36. {
  37. SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
  38. AppViewBackButtonVisibility.Collapsed;
  39. DefaultPivot.SetCheckedButton(DefaultPivotControl.Tab.Map);
  40. if (RouteExample.fromRouteExamp)
  41. {
  42. RouteExample.fromRouteExamp = false;
  43. route = e.Parameter as Route;
  44. Map.MapElements.Clear();
  45. gps.History.Clear();
  46. gps.Refresh();
  47. ShowWaypoints(route);
  48. ShowRoute();
  49. }
  50. }
  51. public async void ShowLocaton(Geopoint _point)
  52. {
  53. Geopoint 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, 17);
  64. }
  65. private async void ShowRoute()
  66. {
  67. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  68. {
  69. Map.Routes.Add(RouteExample.routeView);
  70. });
  71. }
  72. private async void ShowWaypoints(Route route)
  73. {
  74. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  75. {
  76. GeofenceMonitor.Current.Geofences.Clear();
  77. for (int x = 0; x < route.Waypoints.Count; x++)
  78. {
  79. //Waypoints handeling
  80. Waypoint wayp = route.Waypoints.ElementAt(x);
  81. MapIcon wp = new MapIcon { Location = wayp.Position, Title = (x + 1).ToString(), Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/poi.png")) };
  82. Map.MapElements.Add(wp);
  83. //Geofencing handeling
  84. string name = (x + 1).ToString();
  85. if (name == "45")
  86. {
  87. GeofenceMonitor.Current.Geofences.Add(new Geofence(name,
  88. new Geocircle(wayp.Position.Position, 25),
  89. MonitoredGeofenceStates.Entered, false, TimeSpan.FromSeconds(3)));
  90. }
  91. else
  92. {
  93. GeofenceMonitor.Current.Geofences.Add(new Geofence(name, new Geocircle(wayp.Position.Position, 25),
  94. MonitoredGeofenceStates.Entered, true, TimeSpan.FromSeconds(3)));
  95. }
  96. }
  97. });
  98. }
  99. private void Map_OnMapElementClick(MapControl sender, MapElementClickEventArgs args)
  100. {
  101. MapIcon Icon = args.MapElements.FirstOrDefault(x => x is MapIcon) as MapIcon;
  102. for (int x = 0; x < route.Waypoints.Count; x++)
  103. {
  104. if (Icon.Title != "")
  105. {
  106. if (x + 1 == int.Parse(Icon.Title))
  107. {
  108. MainPage.RootFrame.Navigate(typeof(WpDetailPage), new Tuple < Waypoint, Route >(route.Waypoints.ElementAt(x), route));
  109. }
  110. }
  111. }
  112. }
  113. public async void DrawWalkingPath(List<BasicGeoposition> positions)
  114. {
  115. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  116. {
  117. MapPolyline mapPolyline = new MapPolyline();
  118. mapPolyline.StrokeColor = Colors.Blue;
  119. mapPolyline.StrokeThickness = 3;
  120. mapPolyline.Path = new Geopath(positions);
  121. Map.MapElements.Add(mapPolyline);
  122. });
  123. }
  124. private async void OnGeofenceStateChange(GeofenceMonitor sender, object args)
  125. {
  126. var reports = sender.ReadReports();
  127. foreach (GeofenceStateChangeReport report in reports)
  128. {
  129. GeofenceState state = report.NewState;
  130. Geofence geofence = report.Geofence;
  131. if (state == GeofenceState.Entered)
  132. {
  133. foreach (var waypoint in route.Waypoints)
  134. {
  135. int index = waypoint.Title.IndexOf(".");
  136. string number = "";
  137. if (index > 0)
  138. {
  139. number = waypoint.Title.Substring(0, index);
  140. }
  141. if (geofence.Id == number)
  142. {
  143. new Notification("Waypoint", waypoint.Title);
  144. }
  145. }
  146. if (geofence.Id == "45")
  147. {
  148. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  149. {
  150. ButtonPanel.Visibility = Visibility.Visible;
  151. });
  152. }
  153. }
  154. }
  155. }
  156. private void YesButton_OnClick(object sender, RoutedEventArgs e)
  157. {
  158. ButtonPanel.Visibility = Visibility.Collapsed;
  159. MainPage.RootFrame.Navigate(typeof (RouteSelectPage));
  160. Map.MapElements.Clear();
  161. Map.Routes.Clear();
  162. gps.History.Clear();
  163. }
  164. private void NoButton_OnClick(object sender, RoutedEventArgs e)
  165. {
  166. ButtonPanel.Visibility = Visibility.Collapsed;
  167. }
  168. }
  169. }