Gps.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Windows.ApplicationModel.ExtendedExecution;
  5. using Windows.Devices.AllJoyn;
  6. using Windows.Devices.Geolocation;
  7. using Windows.Devices.Geolocation.Geofencing;
  8. using Windows.System;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Media.Animation;
  11. using Breda_Tour.MapScreen;
  12. namespace Breda_Tour.Data
  13. {
  14. public class Gps
  15. {
  16. private ExtendedExecutionSession session;
  17. private MapPage mapPage;
  18. private Geolocator geolocator;
  19. private Geoposition _position;
  20. public Geoposition Position
  21. {
  22. get { return _position; }
  23. }
  24. public List<BasicGeoposition> History { get; set; }
  25. private PositionStatus _status;
  26. public PositionStatus Status
  27. {
  28. get { return _status; }
  29. }
  30. public Gps(MapPage mapPage)
  31. {
  32. History = new List<BasicGeoposition>();
  33. this.mapPage = mapPage;
  34. }
  35. public async void Start()
  36. {
  37. var accessStatus = await Geolocator.RequestAccessAsync();
  38. switch (accessStatus)
  39. {
  40. case GeolocationAccessStatus.Allowed:
  41. geolocator = new Geolocator { DesiredAccuracy = PositionAccuracy.High, MovementThreshold = 3 };
  42. // Subscribe events
  43. geolocator.StatusChanged += OnStatusChanged;
  44. geolocator.PositionChanged += OnPositionChanged;
  45. // Get position
  46. _position = await geolocator.GetGeopositionAsync();
  47. StartLocationExtensionSession();
  48. break;
  49. case GeolocationAccessStatus.Denied:
  50. _status = PositionStatus.NotAvailable;
  51. geolocator = null;
  52. bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));
  53. Start();
  54. break;
  55. case GeolocationAccessStatus.Unspecified:
  56. _status = PositionStatus.NotAvailable;
  57. break;
  58. }
  59. }
  60. private void OnPositionChanged(Geolocator sender, PositionChangedEventArgs args)
  61. {
  62. _position = args.Position;
  63. mapPage.ShowLocaton(_position.Coordinate.Point);
  64. //For route history line
  65. BasicGeoposition BasicG = _position.Coordinate.Point.Position;
  66. History.Add(BasicG);
  67. if (History.Count >= 2)
  68. {
  69. mapPage.DrawWalkingPath(History);
  70. }
  71. }
  72. private void OnStatusChanged(Geolocator sender, StatusChangedEventArgs args)
  73. {
  74. if (args.Status == PositionStatus.Disabled)
  75. {
  76. _position = null;
  77. }
  78. }
  79. public async void Refresh()
  80. {
  81. if (geolocator != null)
  82. {
  83. _position = await geolocator.GetGeopositionAsync();
  84. mapPage.ShowLocaton(_position.Coordinate.Point);
  85. }
  86. }
  87. private async void StartLocationExtensionSession()
  88. {
  89. session = new ExtendedExecutionSession();
  90. session.Description = "Location Tracker";
  91. session.Reason = ExtendedExecutionReason.LocationTracking;
  92. session.Revoked += ExtendedExecutionSession_Revoked;
  93. var result = await session.RequestExtensionAsync();
  94. if (result == ExtendedExecutionResult.Denied)
  95. {
  96. Debug.Write("Denied!!!!");
  97. }
  98. }
  99. private void ExtendedExecutionSession_Revoked(object sender, ExtendedExecutionRevokedEventArgs args)
  100. {
  101. if (session != null)
  102. {
  103. session.Dispose();
  104. session = null;
  105. }
  106. }
  107. }
  108. }