AR + GPS Location  3.0.0
All Classes Namespaces Functions Variables Properties Events Pages
UnityLocationProvider.cs
1 using UnityEngine;
2 using M = System.Math;
3 
4 namespace ARLocation
5 {
6 
8  {
9  private float androidMagneticDeclination;
10  private AndroidNativeCompass androidNativeCompass;
11  public override string Name => "UnityLocationProvider";
12 
13  public override bool IsCompassEnabled => Input.compass.enabled;
14 
15  protected override void RequestLocationAndCompassUpdates()
16  {
17  // Debug.Log("[UnityLocationProvider]: Requesting location updates...");
18 
19  Input.compass.enabled = true;
20 
21  Input.location.Start(
22  (float)Options.AccuracyRadius,
24  );
25  }
26 
27  protected override void InnerOnEnabled()
28  {
29  androidMagneticDeclination = AndroidMagneticDeclination.GetDeclination(CurrentLocation.ToLocation());
30  androidNativeCompass = new AndroidNativeCompass((float) (1.0 - LowPassFilterFactor));
31  }
32 
33  protected override void UpdateLocationRequestStatus()
34  {
35  switch (Input.location.status)
36  {
37  case LocationServiceStatus.Initializing:
38  Status = LocationProviderStatus.Initializing;
39  break;
40 
41  case LocationServiceStatus.Failed:
42  Status = LocationProviderStatus.Failed;
43  break;
44 
45  case LocationServiceStatus.Running:
46  Status = LocationProviderStatus.Started;
47  break;
48 
49  case LocationServiceStatus.Stopped:
50  Status = LocationProviderStatus.Idle;
51  break;
52  }
53  }
54 
55  protected override LocationReading? ReadLocation()
56  {
57  if (!HasStarted)
58  {
59  return null;
60  }
61 
62  var data = Input.location.lastData;
63 
64  return new LocationReading()
65  {
66  latitude = data.latitude,
67  longitude = data.longitude,
68  altitude = data.altitude,
69  accuracy = data.horizontalAccuracy,
70  floor = -1,
71  timestamp = (long)(data.timestamp * 1000)
72  };
73  }
74 
75  protected override HeadingReading? ReadHeading()
76  {
77  if (!HasStarted)
78  {
79  return null;
80  }
81 
82  // ReSharper disable once RedundantAssignment
83  var magneticHeading = Input.compass.magneticHeading;
84 
85  // ReSharper disable once RedundantAssignment
86  var trueHeading = Input.compass.trueHeading;
87 
88 #if PLATFORM_ANDROID
89  var tiltCorrectedMagneticHeading = GetMagneticHeading();
90  magneticHeading = tiltCorrectedMagneticHeading;
91  trueHeading = tiltCorrectedMagneticHeading + androidMagneticDeclination;
92 #endif
93 
94  if (trueHeading < 0)
95  {
96  trueHeading += 360;
97  }
98 
99  return new HeadingReading()
100  {
101  heading = trueHeading,
102  magneticHeading = magneticHeading,
103  accuracy = Input.compass.headingAccuracy,
104  timestamp = (long)(Input.compass.timestamp * 1000),
105  isMagneticHeadingAvailable = Input.compass.enabled
106  };
107  }
108 
109  private float GetMagneticHeading()
110  {
111 #if PLATFORM_ANDROID
112  if (!SystemInfo.supportsGyroscope || !ApplyCompassTiltCompensationOnAndroid || androidNativeCompass == null)
113  {
114  return Input.compass.magneticHeading;
115  }
116 
117  return androidNativeCompass.GetMagneticHeading();
118 
119 // if (Screen.orientation == ScreenOrientation.Landscape)
120 // {
121 // return heading;// + 45;
122 // }
123 // else
124 // {
125 // return heading;
126 // }
127 
128 #else
129  return Input.compass.magneticHeading;
130 #endif
131  }
132  }
133 }
ARLocation.AndroidNativeCompass
Definition: AndroidNativeCompass.cs:8
ARLocation.AbstractLocationProvider.Status
LocationProviderStatus Status
Gets or sets the current status of the location provider.
Definition: AbstractLocationProvider.cs:72
ARLocation.UnityLocationProvider.RequestLocationAndCompassUpdates
override void RequestLocationAndCompassUpdates()
Requests the location and compass updates from the device; should be implemented by each provider.
Definition: UnityLocationProvider.cs:15
ARLocation.UnityLocationProvider
Definition: UnityLocationProvider.cs:8
ARLocation.AbstractLocationProvider.CurrentLocation
LocationReading CurrentLocation
Gets or sets the current location.
Definition: AbstractLocationProvider.cs:34
ARLocation.LocationProviderOptions.MinDistanceBetweenUpdates
double MinDistanceBetweenUpdates
The minimum distance between consecutive location updates, in meters.
Definition: ILocationProvider.cs:21
ARLocation.UnityLocationProvider.UpdateLocationRequestStatus
override void UpdateLocationRequestStatus()
Updates the location service status from the device; should be implemented by each provider.
Definition: UnityLocationProvider.cs:33
ARLocation.AbstractLocationProvider
Abstract location provider. All concrete location provider implementations should derive from this.
Definition: AbstractLocationProvider.cs:15
ARLocation.AbstractLocationProvider.Options
LocationProviderOptions Options
The options of the location provider.
Definition: AbstractLocationProvider.cs:28
ARLocation.UnityLocationProvider.ReadLocation
override? LocationReading ReadLocation()
Reads the location from the device; should be implemented by each provider.
Definition: UnityLocationProvider.cs:55
ARLocation.HeadingReading
Definition: HeadingReading.cs:5
ARLocation.LocationProviderOptions.AccuracyRadius
double AccuracyRadius
The minimum accuracy of accepted location measurements, in meters.
Definition: ILocationProvider.cs:30
ARLocation.LocationReading
Definition: LocationReading.cs:5
ARLocation
Definition: ARLocationConfigInspector.cs:7
ARLocation.UnityLocationProvider.ReadHeading
override? HeadingReading ReadHeading()
Reads the heading from the device; should be implemented by each provider.
Definition: UnityLocationProvider.cs:75