AR + GPS Location  3.0.0
All Classes Namespaces Functions Variables Properties Events Pages
ARLocationProvider.cs
1 using System;
2 using System.Collections;
3 using UnityEngine;
4 using UnityEngine.Events;
5 using UnityEngine.Serialization;
6 // ReSharper disable UnusedMember.Global
7 
8 
9 namespace ARLocation
10 {
11  using Utils;
12 
13  [AddComponentMenu("AR+GPS/AR Location Provider")]
14  [HelpURL("https://http://docs.unity-ar-gps-location.com/guide/#arlocationprovider")]
15  [DisallowMultipleComponent]
16  public class ARLocationProvider : Singleton<ARLocationProvider>
17  {
18  [Serializable]
19  public class LocationEnabledUnityEvent : UnityEvent<Location> {}
20  [Serializable]
21  public class LocationUpdatedUnityEvent : UnityEvent<Location> {}
22 
23  [Serializable]
24  public class CompassUpdatedUnityEvent: UnityEvent<HeadingReading> {}
25 
26  [FormerlySerializedAs("LocationUpdateSettings")]
27  [Tooltip("The options for the Location Provider.")]
28  [Header("Update Settings")]
29  public LocationProviderOptions LocationProviderSettings = new LocationProviderOptions();
30 
31  [Tooltip("The data of mock location. If present, overrides the Mock Location above.")]
32  [Header("Mock Data")]
33  public LocationData MockLocationData;
34 
35  [Tooltip("The maximum wait time to wait for location initialization.")]
36  [Header("Initialization")]
37  public uint MaxWaitTime = 200;
38 
39  [Tooltip("Wait this many seconds before starting location services. Useful when using Unity Remote.")]
40  public uint StartUpDelay;
41 
42  [Header("Debug")]
43  [Tooltip("When debug mode is enabled, this component will print relevant messages to the console. Filter by 'ARLocationProvider' in the log output to see the messages.")]
44  public bool DebugMode;
45 
46  [Header("Events")]
47 
48  [Tooltip("Called after the first location is read.")]
50 
51  [Tooltip("Called after each new location update.")]
52  public LocationUpdatedUnityEvent OnLocationUpdated = new LocationUpdatedUnityEvent();
53 
54  [Tooltip("Called after each new raw device GPS data is obtained.")]
55  public LocationUpdatedUnityEvent OnRawLocationUpdated = new LocationUpdatedUnityEvent();
56 
57  [Tooltip("Called after each new compass update.")]
58  public CompassUpdatedUnityEvent OnCompassUpdated = new CompassUpdatedUnityEvent();
59 
63  public ILocationProvider Provider { get; private set; }
64 
68  public bool IsEnabled => Provider.IsEnabled;
69 
73  public bool HasStarted => Provider.HasStarted;
74 
78  public int LocationUpdateCount => Provider.LocationUpdateCount;
79 
83  public bool IsPaused => Provider.Paused;
84 
88  public LocationReading CurrentLocation => Provider.CurrentLocation;
89 
93  public LocationReading LastLocation => Provider.LastLocation;
94 
98  public HeadingReading CurrentHeading => Provider.CurrentHeading;
99 
100 
104  public float TimeSinceStart => Time.time - Provider.StartTime;
105 
109  public double DistanceFromStartPoint => Provider.DistanceFromStartPoint;
110 
111  private int measurementCount;
112  private bool mute;
113 
114  public event LocationUpdatedDelegate OnLocationUpdatedDelegate;
115  public event CompassUpdateDelegate OnCompassUpdateDelegate;
116  public event Action OnRestartDelegate;
117 
118  private Vector3 cameraPositionAtLastUpdate;
119  public override void Awake()
120  {
121  base.Awake();
122 
123 #if UNITY_EDITOR
125 
126  if (MockLocationData != null)
127  {
128  Logger.LogFromMethod("ARLocationProvider", "Awake", $"Using mock location {MockLocationData}", DebugMode);
129  ((MockLocationProvider) Provider).mockLocation = MockLocationData.Location;
130  }
131 #elif ARGPS_CUSTOM_PROVIDER
132  // If you want to use a custom location provider, add 'ARGPS_CUSTOM_PROVIDER' to the define symbols in the Player
133  // settings, create a implementation of ILocationProvider, and instantiate it in the line below.
134  Provider = new ARGpsCustomLocationProvider();
135 #else
137 #endif
138  Logger.LogFromMethod("ARLocationProvider", "Awake",": Using provider " + Provider.Name, DebugMode);
139  }
140 
141 
142 
143  private void InitProviderEventListeners()
144  {
145  Logger.LogFromMethod("ARLocationProvider", "InitProviderEventListeners","Initializing location provider listeners.", DebugMode);
146 
147  Provider.LocationUpdated += Provider_LocationUpdated;
148  Provider.CompassUpdated += Provider_CompassUpdated;
149  Provider.LocationUpdatedRaw += ProviderOnLocationUpdatedRaw;
150 
151  Provider.OnEnabled(OnProviderEnabledDelegate);
152 
153  if (Provider.IsEnabled)
154  {
156  }
157  }
158 
159  private void ProviderOnLocationUpdatedRaw(LocationReading currentLocation, LocationReading lastLocation)
160  {
161  OnRawLocationUpdated?.Invoke(currentLocation.ToLocation());
162  }
163 
164  private void OnProviderEnabledDelegate()
165  {
166  Logger.LogFromMethod("ARLocationProvider", "OnProviderEnabledDelegate","Provider enabled; emitting 'OnEnabled' event.", DebugMode);
167  OnEnabled?.Invoke(CurrentLocation.ToLocation());
168  }
169 
170  IEnumerator Start()
171  {
172  InitProviderEventListeners();
173 
174  Provider.Options = LocationProviderSettings;
175 
176  Logger.LogFromMethod("ARLocationProvider", "Start","Starting the location provider", DebugMode);
177  yield return StartCoroutine(Provider.Start(MaxWaitTime, StartUpDelay));
178  }
179 
180  public void Mute()
181  {
182  Logger.LogFromMethod("ARLocationProvider", "Mute","Muting ARLocationProvider.", DebugMode);
183  mute = true;
184  }
185 
186  public void Unmute(bool emit = true)
187  {
188  Logger.LogFromMethod("ARLocationProvider", "Mute","Un-muting ARLocationProvider.", DebugMode);
189  mute = false;
190  if (Provider.IsEnabled && emit) ForceLocationUpdate();
191  }
192 
193  private void Provider_CompassUpdated(HeadingReading heading, HeadingReading lastReading)
194  {
195  if (mute) return;
196 
197  OnCompassUpdateDelegate?.Invoke(heading, lastReading);
198  OnCompassUpdated?.Invoke(heading);
199  }
200 
201  private void Provider_LocationUpdated(LocationReading currentLocation, LocationReading lastLocation)
202  {
203  if (mute) return;
204 
205  measurementCount++;
206 
207  if ((LocationProviderSettings.MaxNumberOfUpdates > 0) && (measurementCount >= LocationProviderSettings.MaxNumberOfUpdates))
208  {
209  Provider.Pause();
210  }
211 
212  Logger.LogFromMethod("ARLocationProvider", "Provider_LocationUpdated",$"New location {currentLocation}.", DebugMode);
213 
214  cameraPositionAtLastUpdate = ARLocationManager.Instance.MainCamera.transform.position;
215  OnLocationUpdatedDelegate?.Invoke(currentLocation, lastLocation);
216  OnLocationUpdated?.Invoke(currentLocation.ToLocation());
217  }
218 
223  public void ForceLocationUpdate()
224  {
225  Logger.LogFromMethod("ARLocationProvider", "ForceLocationUpdate","Emitting a forced location update", DebugMode);
226  Provider.ForceLocationUpdate();
227  }
228 
229  void Update()
230  {
231  if (Provider == null || !Provider.HasStarted)
232  {
233  return;
234  }
235 
236  Provider.Update();
237  }
238 
242  public void Pause()
243  {
244  Logger.LogFromMethod("ARLocationProvider", "Pause","Pausing the location provider.", DebugMode);
245  Provider?.Pause();
246  }
247 
251  public void Resume()
252  {
253  Logger.LogFromMethod("ARLocationProvider", "Resume","Resuming the location provider.", DebugMode);
254  Provider?.Resume();
255  }
256 
260  public void Restart()
261  {
262  Logger.LogFromMethod("ARLocationProvider", "Restart","Restarting the location provider.", DebugMode);
263  Provider?.Restart();
264  OnRestartDelegate?.Invoke();
265  }
266 
276  public void OnLocationUpdatedEvent(LocationUpdatedDelegate locationUpdatedDelegate, bool useRawIfEnabled = false)
277  {
278  if (IsEnabled)
279  {
280  locationUpdatedDelegate(CurrentLocation, useRawIfEnabled ? Provider.LastLocationRaw : LastLocation);
281  }
282 
283  OnLocationUpdatedDelegate += locationUpdatedDelegate;
284  }
285 
286  public void OnProviderRestartEvent(Action del)
287  {
288  OnRestartDelegate += del;
289  }
290 
295  public void OnCompassUpdatedEvent(CompassUpdateDelegate compassUpdateDelegate)
296  {
297  OnCompassUpdateDelegate += compassUpdateDelegate;
298  }
299 
304  public void OnEnabledEvent(LocationEnabledDelegate del)
305  {
306  Provider.OnEnabled(del);
307  }
308 
313  public void OnFailedEvent(LocationFailedDelegate del)
314  {
315  Provider.OnFail(del);
316  }
317 
323  public Location GetLocationForWorldPosition(Vector3 position)
324  {
325  var cameraDelta = ARLocationManager.Instance.MainCamera.transform.position - cameraPositionAtLastUpdate;
326  return Location.GetLocationForWorldPosition(gameObject.transform, cameraPositionAtLastUpdate, CurrentLocation.ToLocation(), position - cameraDelta);
327  }
328  }
329 }
ARLocation.ARLocationProvider.IsEnabled
bool IsEnabled
If true, the location provider has received the first location data.
Definition: ARLocationProvider.cs:68
ARLocation.ARLocationProvider.OnFailedEvent
void OnFailedEvent(LocationFailedDelegate del)
Register a delegate for when the provider fails to initialize location services.
Definition: ARLocationProvider.cs:313
ARLocation.ARLocationProvider
Definition: ARLocationProvider.cs:17
ARLocation.LocationData
Data used to construct a spline passing trough a set of geographical locations.
Definition: LocationData.cs:12
ARLocation.ARLocationProvider.GetLocationForWorldPosition
Location GetLocationForWorldPosition(Vector3 position)
Given a world position vector, return the corresponding geographical Location.
Definition: ARLocationProvider.cs:323
ARLocation.ARLocationManager
This Component manages all positioned GameObjects, synchronizing their world position in the scene wi...
Definition: ARLocationManager.cs:30
ARLocation.ARLocationProvider.LocationUpdateCount
int LocationUpdateCount
The number of location updates so far.
Definition: ARLocationProvider.cs:78
ARLocation.MockLocationProvider
Definition: MockLocationProvider.cs:7
ARLocation.ARLocationProvider.OnCompassUpdatedEvent
void OnCompassUpdatedEvent(CompassUpdateDelegate compassUpdateDelegate)
Register a delegate to compass/heading updates.
Definition: ARLocationProvider.cs:295
ARLocation.ARLocationProvider.DistanceFromStartPoint
double DistanceFromStartPoint
The distance from the initial measured position.
Definition: ARLocationProvider.cs:109
ARLocation.Location
Represents a geographical location.
Definition: Location.cs:19
ARLocation.Utils.Singleton.Instance
static T Instance
Access singleton instance through this propriety.
Definition: Singleton.cs:18
ARLocation.ARLocationProvider.ForceLocationUpdate
void ForceLocationUpdate()
Force the provider to emit a location update event. This wont force a new read of location,...
Definition: ARLocationProvider.cs:223
ARLocation.UnityLocationProvider
Definition: UnityLocationProvider.cs:8
ARLocation.ARLocationProvider.OnLocationUpdatedEvent
void OnLocationUpdatedEvent(LocationUpdatedDelegate locationUpdatedDelegate, bool useRawIfEnabled=false)
Register a delegate to location updates.
Definition: ARLocationProvider.cs:276
ARLocation.ARLocationProvider.OnEnabledEvent
void OnEnabledEvent(LocationEnabledDelegate del)
RegisterRegister delegate for when the provider enables location updates.
Definition: ARLocationProvider.cs:304
ARLocation.ARLocationProvider.Resume
void Resume()
Resumes location updates
Definition: ARLocationProvider.cs:251
ARLocation.ARLocationProvider.Pause
void Pause()
Pauses location updates
Definition: ARLocationProvider.cs:242
ARLocation.ARLocationProvider.LocationEnabledUnityEvent
Definition: ARLocationProvider.cs:19
ARLocation.ARLocationProvider.TimeSinceStart
float TimeSinceStart
Time since the location provider has started.
Definition: ARLocationProvider.cs:104
ARLocation.ILocationProvider
Definition: ILocationProvider.cs:52
ARLocation.Utils.Singleton
Definition: Singleton.cs:8
ARLocation.Location.GetLocationForWorldPosition
static Location GetLocationForWorldPosition(Transform arLocationRoot, Vector3 center, Location userLocation, Vector3 worldPosition)
Calculates the wgs84 Location for a given world position vector.
Definition: Location.cs:324
ARLocation.LocationProviderOptions
Definition: ILocationProvider.cs:10
ARLocation.ARLocationProvider.HasStarted
bool HasStarted
If true, the location provider has started, but no location data has been read.
Definition: ARLocationProvider.cs:73
ARLocation.ARLocationProvider.Provider
ILocationProvider Provider
Returns the current location provider.
Definition: ARLocationProvider.cs:63
ARLocation.HeadingReading
Definition: HeadingReading.cs:5
ARLocation.ARLocationProvider.CurrentLocation
LocationReading CurrentLocation
The latest location data.
Definition: ARLocationProvider.cs:88
ARLocation.LocationData.Location
Location Location
The geographical locations that the path will interpolate.
Definition: LocationData.cs:17
ARLocation.ARLocationProvider.CurrentHeading
HeadingReading CurrentHeading
The current heading data.
Definition: ARLocationProvider.cs:98
ARLocation.ARLocationProvider.IsPaused
bool IsPaused
If true, updates are paused.
Definition: ARLocationProvider.cs:83
ARLocation.LocationReading
Definition: LocationReading.cs:5
ARLocation
Definition: ARLocationConfigInspector.cs:7
ARLocation.ARLocationProvider.LocationUpdatedUnityEvent
Definition: ARLocationProvider.cs:21
ARLocation.ARLocationProvider.CompassUpdatedUnityEvent
Definition: ARLocationProvider.cs:24
ARLocation.ARLocationProvider.Restart
void Restart()
Resets the location provider.
Definition: ARLocationProvider.cs:260
ARLocation.ARLocationProvider.LastLocation
LocationReading LastLocation
The previous location data.
Definition: ARLocationProvider.cs:93