UnityLocationProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using UnityEngine;
  2. namespace ARLocation
  3. {
  4. public class UnityLocationProvider : AbstractLocationProvider
  5. {
  6. private float androidMagneticDeclination;
  7. private AndroidNativeCompass androidNativeCompass;
  8. public override string Name => "UnityLocationProvider";
  9. public override bool IsCompassEnabled => Input.compass.enabled;
  10. protected override void RequestLocationAndCompassUpdates()
  11. {
  12. // Debug.Log("[UnityLocationProvider]: Requesting location updates...");
  13. Input.compass.enabled = true;
  14. Input.location.Start(
  15. (float)Options.AccuracyRadius,
  16. (float)Options.MinDistanceBetweenUpdates
  17. );
  18. }
  19. protected override void InnerOnEnabled()
  20. {
  21. androidMagneticDeclination = AndroidMagneticDeclination.GetDeclination(CurrentLocation.ToLocation());
  22. androidNativeCompass = new AndroidNativeCompass((float) (1.0 - LowPassFilterFactor));
  23. }
  24. protected override void UpdateLocationRequestStatus()
  25. {
  26. switch (Input.location.status)
  27. {
  28. case LocationServiceStatus.Initializing:
  29. Status = LocationProviderStatus.Initializing;
  30. break;
  31. case LocationServiceStatus.Failed:
  32. Status = LocationProviderStatus.Failed;
  33. break;
  34. case LocationServiceStatus.Running:
  35. Status = LocationProviderStatus.Started;
  36. break;
  37. case LocationServiceStatus.Stopped:
  38. Status = LocationProviderStatus.Idle;
  39. break;
  40. }
  41. }
  42. protected override LocationReading? ReadLocation()
  43. {
  44. if (!HasStarted)
  45. {
  46. return null;
  47. }
  48. var data = Input.location.lastData;
  49. return new LocationReading()
  50. {
  51. latitude = data.latitude,
  52. longitude = data.longitude,
  53. altitude = data.altitude,
  54. accuracy = data.horizontalAccuracy,
  55. floor = -1,
  56. timestamp = (long)(data.timestamp * 1000)
  57. };
  58. }
  59. protected override HeadingReading? ReadHeading()
  60. {
  61. if (!HasStarted)
  62. {
  63. return null;
  64. }
  65. // ReSharper disable once RedundantAssignment
  66. var magneticHeading = Input.compass.magneticHeading;
  67. // ReSharper disable once RedundantAssignment
  68. var trueHeading = Input.compass.trueHeading;
  69. #if PLATFORM_ANDROID
  70. var tiltCorrectedMagneticHeading = GetMagneticHeading();
  71. magneticHeading = tiltCorrectedMagneticHeading;
  72. trueHeading = tiltCorrectedMagneticHeading + androidMagneticDeclination;
  73. #endif
  74. if (trueHeading < 0)
  75. {
  76. trueHeading += 360;
  77. }
  78. return new HeadingReading()
  79. {
  80. heading = trueHeading,
  81. magneticHeading = magneticHeading,
  82. accuracy = Input.compass.headingAccuracy,
  83. timestamp = (long)(Input.compass.timestamp * 1000),
  84. isMagneticHeadingAvailable = Input.compass.enabled
  85. };
  86. }
  87. private float GetMagneticHeading()
  88. {
  89. #if PLATFORM_ANDROID
  90. if (!SystemInfo.supportsGyroscope || !ApplyCompassTiltCompensationOnAndroid || androidNativeCompass == null)
  91. {
  92. return Input.compass.magneticHeading;
  93. }
  94. return androidNativeCompass.GetMagneticHeading();
  95. // if (Screen.orientation == ScreenOrientation.Landscape)
  96. // {
  97. // return heading;// + 45;
  98. // }
  99. // else
  100. // {
  101. // return heading;
  102. // }
  103. #else
  104. return Input.compass.magneticHeading;
  105. #endif
  106. }
  107. }
  108. }