MapPage.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Breda_Tour.Data;
  15. using Breda_Tour.RouteSelectScreen;
  16. namespace Breda_Tour.MapScreen
  17. {
  18. public sealed partial class MapPage : Page
  19. {
  20. private MapIcon marker;
  21. private Gps gps;
  22. private Route route;
  23. public MapPage()
  24. {
  25. GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChange;
  26. this.NavigationCacheMode = NavigationCacheMode.Enabled;
  27. marker = new MapIcon();
  28. marker.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Marker.png"));
  29. marker.NormalizedAnchorPoint = new Point(0.5, 0.5);
  30. gps = new Gps(this);
  31. gps.Start();
  32. this.InitializeComponent();
  33. }
  34. protected override void OnNavigatedTo(NavigationEventArgs e)
  35. {
  36. SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
  37. AppViewBackButtonVisibility.Collapsed;
  38. DefaultPivot.SetCheckedButton(DefaultPivotControl.Tab.Map);
  39. if (RouteExample.fromRouteExamp)
  40. {
  41. RouteExample.fromRouteExamp = false;
  42. route = e.Parameter as Route;
  43. Map.MapElements.Clear();
  44. gps.History.Clear();
  45. gps.Refresh();
  46. ShowWaypoints(route);
  47. ShowRoute();
  48. }
  49. }
  50. public async void ShowLocaton(Geopoint _point)
  51. {
  52. Geopoint point = _point;
  53. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  54. {
  55. Map.Center = point;
  56. if (!Map.MapElements.Contains(marker))
  57. {
  58. Map.MapElements.Add(marker);
  59. }
  60. marker.Location = point;
  61. });
  62. await Map.TrySetViewAsync(point, 17);
  63. }
  64. private async void ShowRoute()
  65. {
  66. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  67. {
  68. Map.Routes.Add(RouteExample.routeView);
  69. });
  70. }
  71. private async void ShowWaypoints(Route route)
  72. {
  73. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  74. {
  75. GeofenceMonitor.Current.Geofences.Clear();
  76. for (int x = 0; x < route.Waypoints.Count; x++)
  77. {
  78. //Waypoints handeling
  79. Waypoint wayp = route.Waypoints.ElementAt(x);
  80. MapIcon wp = new MapIcon { Location = wayp.Position, Title = (x + 1).ToString() };
  81. Map.MapElements.Add(wp);
  82. //Geofencing handeling
  83. string name = (x + 1).ToString();
  84. GeofenceMonitor.Current.Geofences.Add(new Geofence(name, new Geocircle(wayp.Position.Position, 25),
  85. MonitoredGeofenceStates.Entered, true, TimeSpan.FromSeconds(3)));
  86. }
  87. });
  88. }
  89. private void Map_OnMapElementClick(MapControl sender, MapElementClickEventArgs args)
  90. {
  91. MapIcon Icon = args.MapElements.FirstOrDefault(x => x is MapIcon) as MapIcon;
  92. for (int x = 0; x < route.Waypoints.Count; x++)
  93. {
  94. if (Icon.Title != "")
  95. {
  96. if (x + 1 == int.Parse(Icon.Title))
  97. {
  98. MainPage.RootFrame.Navigate(typeof(WpDetailPage), new Tuple < Waypoint, Route >(route.Waypoints.ElementAt(x), route));
  99. }
  100. }
  101. }
  102. }
  103. public async void DrawWalkingPath(List<BasicGeoposition> positions)
  104. {
  105. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  106. {
  107. MapPolyline mapPolyline = new MapPolyline();
  108. mapPolyline.StrokeColor = Colors.Blue;
  109. mapPolyline.StrokeThickness = 3;
  110. mapPolyline.Path = new Geopath(positions);
  111. Map.MapElements.Add(mapPolyline);
  112. });
  113. }
  114. private void OnGeofenceStateChange(GeofenceMonitor sender, object args)
  115. {
  116. var reports = sender.ReadReports();
  117. foreach (GeofenceStateChangeReport report in reports)
  118. {
  119. GeofenceState state = report.NewState;
  120. Geofence geofence = report.Geofence;
  121. if (state == GeofenceState.Entered)
  122. {
  123. foreach (var waypoint in route.Waypoints)
  124. {
  125. int index = waypoint.Title.IndexOf(".");
  126. string number = "";
  127. if (index > 0)
  128. {
  129. number = waypoint.Title.Substring(0, index);
  130. }
  131. if (geofence.Id == number)
  132. {
  133. new Notification("Waypoint", waypoint.Title);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }