GeoHandler.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Windows.Devices.Geolocation;
  6. using Windows.Devices.Geolocation.Geofencing;
  7. using Windows.System;
  8. using YJMPD_UWP.Helpers.EventArgs;
  9. namespace YJMPD_UWP.Model
  10. {
  11. public class GeoHandler
  12. {
  13. private Geolocator geo;
  14. private PositionStatus _status;
  15. public PositionStatus Status { get { return _status; } }
  16. private Geoposition _position;
  17. public Geoposition Position { get { return _position; } }
  18. public bool? Connected { get; private set; }
  19. private bool trackhistory = false;
  20. private List<Geoposition> _history;
  21. public List<Geoposition> History
  22. {
  23. get
  24. {
  25. return _history;
  26. }
  27. }
  28. //Events
  29. public delegate void OnPositionUpdateHandler(object sender, PositionUpdatedEventArgs e);
  30. public event OnPositionUpdateHandler OnPositionUpdate;
  31. public delegate void OnStatusUpdateHandler(object sender, PositionStatusUpdatedEventArgs e);
  32. public event OnStatusUpdateHandler OnStatusUpdate;
  33. public GeoHandler()
  34. {
  35. _status = PositionStatus.NotInitialized;
  36. Connected = false;
  37. _history = new List<Geoposition>();
  38. StartTracking();
  39. }
  40. public async void ForceRefresh()
  41. {
  42. if (geo == null)
  43. await StartTracking();
  44. else
  45. _position = await geo.GetGeopositionAsync();
  46. }
  47. public async void TryConnectIfNull()
  48. {
  49. if (geo == null)
  50. await StartTracking();
  51. }
  52. public void ClearHistory()
  53. {
  54. trackhistory = false;
  55. _history.Clear();
  56. }
  57. public void KeepHistory()
  58. {
  59. trackhistory = true;
  60. }
  61. public async Task<String> StartTracking()
  62. {
  63. // Request permission to access location
  64. if (Status != PositionStatus.NotAvailable && Status != PositionStatus.NotInitialized)
  65. return "Already Connected";
  66. var accessStatus = await Geolocator.RequestAccessAsync();
  67. switch (accessStatus)
  68. {
  69. case GeolocationAccessStatus.Allowed:
  70. geo = new Geolocator
  71. {
  72. DesiredAccuracy = PositionAccuracy.High,
  73. MovementThreshold = 3
  74. //ReportInterval = 1500
  75. };
  76. ClearHistory();
  77. Connected = true;
  78. geo.PositionChanged += Geo_PositionChanged;
  79. geo.StatusChanged += Geo_StatusChanged;
  80. GeofenceMonitor.Current.Geofences.Clear();
  81. _position = await geo.GetGeopositionAsync();
  82. return "Connected";
  83. case GeolocationAccessStatus.Denied:
  84. Connected = false;
  85. _status = PositionStatus.NotAvailable;
  86. bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));
  87. return "Denied";
  88. default:
  89. case GeolocationAccessStatus.Unspecified:
  90. Connected = false;
  91. _status = PositionStatus.NotAvailable;
  92. return "Error";
  93. }
  94. }
  95. private void Geo_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
  96. {
  97. if (args.Status == PositionStatus.Disabled)
  98. {
  99. Connected = false;
  100. _position = null;
  101. }
  102. else if (!(bool)Connected)
  103. Connected = true;
  104. UpdateStatus(args.Status);
  105. }
  106. private void Geo_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
  107. {
  108. UpdatePosition(args.Position);
  109. if(trackhistory)
  110. _history.Add(args.Position);
  111. }
  112. private void UpdateStatus(PositionStatus s)
  113. {
  114. _status = s;
  115. if (OnStatusUpdate == null) return;
  116. OnStatusUpdate(this, new PositionStatusUpdatedEventArgs(s));
  117. }
  118. private void UpdatePosition(Geoposition pos)
  119. {
  120. _position = pos;
  121. if (OnPositionUpdate == null) return;
  122. OnPositionUpdate(this, new PositionUpdatedEventArgs(pos));
  123. }
  124. }
  125. }