Gps.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using Windows.Devices.AllJoyn;
  4. using Windows.Devices.Geolocation;
  5. using Windows.System;
  6. using Windows.UI.Xaml;
  7. using Breda_Tour.MapScreen;
  8. namespace Breda_Tour.Data
  9. {
  10. public class Gps
  11. {
  12. private MapPage mapPage;
  13. private Geolocator geolocator;
  14. private Geoposition _position;
  15. public Geoposition Position
  16. {
  17. get { return _position; }
  18. }
  19. private List<Geoposition> _history;
  20. public List<Geoposition> History
  21. {
  22. get { return _history; }
  23. }
  24. private PositionStatus _status;
  25. public PositionStatus Status
  26. {
  27. get { return _status; }
  28. }
  29. public Gps(MapPage mapPage)
  30. {
  31. _history = new List<Geoposition>();
  32. this.mapPage = mapPage;
  33. }
  34. public async void Start()
  35. {
  36. var accessStatus = await Geolocator.RequestAccessAsync();
  37. switch (accessStatus)
  38. {
  39. case GeolocationAccessStatus.Allowed:
  40. geolocator = new Geolocator { DesiredAccuracyInMeters = 1, MovementThreshold = 2 };
  41. // Subscribe events
  42. geolocator.StatusChanged += OnStatusChanged;
  43. geolocator.PositionChanged += OnPositionChanged;
  44. // Get position
  45. _position = await geolocator.GetGeopositionAsync();
  46. break;
  47. case GeolocationAccessStatus.Denied:
  48. _status = PositionStatus.NotAvailable;
  49. bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));
  50. break;
  51. case GeolocationAccessStatus.Unspecified:
  52. _status = PositionStatus.NotAvailable;
  53. break;
  54. }
  55. }
  56. private void OnPositionChanged(Geolocator sender, PositionChangedEventArgs args)
  57. {
  58. mapPage.ShowLocaton(args.Position.Coordinate.Point);
  59. _position = args.Position;
  60. }
  61. private void OnStatusChanged(Geolocator sender, StatusChangedEventArgs args)
  62. {
  63. if (args.Status == PositionStatus.Disabled)
  64. {
  65. _position = null;
  66. }
  67. }
  68. public async void Refresh()
  69. {
  70. if (geolocator == null)
  71. {
  72. Start();
  73. }
  74. else
  75. {
  76. _position = await geolocator.GetGeopositionAsync();
  77. }
  78. }
  79. }
  80. }