cancel
Showing results for 
Search instead for 
Did you mean: 

Using Location Services API in Unity

billfr5150
Honored Guest

Has anyone successfully used Unity's Location Services API for Quest 3?  

Here's the code that I'm using:

IEnumerator LocationCoroutine()
{
if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.FineLocation)) {
UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.FineLocation);
}

// First, check if user has location service enabled
if (!UnityEngine.Input.location.isEnabledByUser) {
yield break;
}

// Start service before querying location
Input.location.Start(1f, 1f);

MessageText.text = "<< Connecting to Location Services >>";

// Wait until service initializes
int maxWait = 15;
while (UnityEngine.Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSecondsRealtime(1);
maxWait--;
}

// Service didn't initialize in 15 seconds
if (maxWait < 1)
{
MessageText.text = "<< Timed out >>";
yield break;
}

// Connection has failed
if (UnityEngine.Input.location.status != LocationServiceStatus.Running)
{
MessageText.text = "<< Unable to determine device location. Failed with status {0}" + UnityEngine.Input.location.status + " >>";
yield break;
}
else
{

Debug.LogFormat("Location: "
+ UnityEngine.Input.location.lastData.latitude + " "
+ UnityEngine.Input.location.lastData.longitude + " "
+ UnityEngine.Input.location.lastData.altitude + " "
+ UnityEngine.Input.location.lastData.horizontalAccuracy + " "
+ UnityEngine.Input.location.lastData.timestamp);
}

// Stop service if there is no need to query location updates continuously
Input.location.Stop();
StopCoroutine(LocationCoroutine());
}

0 REPLIES 0