2 years ago
#63180
Choppa200
Unity Library: Android GPS Access Issue
I am having issues with the unity library when it comes to accessing GPS Data from my phone. I have debugged it and it goes to LocationServiceStatus.stopped. But I get 0's for coordinates so I don't know why I am getting 0's and what would be calling that issue. I have also have tested this on my phone and the pop-up asking to request permission to access my location works as well but still get 0's for my coordinates. Any help on this would be appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GPSLocation : MonoBehaviour
{
public TextMeshProUGUI GPSStatus;
public TextMeshProUGUI latitudeValue;
public TextMeshProUGUI longitudeValue;
public TextMeshProUGUI altitudeValue;
public TextMeshProUGUI horizontalAccuracyValue;
public TextMeshProUGUI TimestampValue;
// Start is called before the first frame update
void Start()
{
StartCoroutine(GPSLoc());
}
IEnumerator GPSLoc()
{
#if UNITY_EDITOR
// No permission handling needed in Editor
#elif UNITY_ANDROID
if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.FineLocation))
{
UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.FineLocation);
UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.CoarseLocation);
GPSStatus.text = "GRANTED";
}
else
{
GPSStatus.text = "No Access";
Debug.LogFormat("Android Does not have access");
yield break;
}
//if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.Camera))
//{
// UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.Camera);
//}
//else
//{
// Debug.LogFormat("Android Does not have camera access");
// yield break;
//}
// First, check if user has location service enabled
//if (!UnityEngine.Input.location.isEnabledByUser)
//{
// // TODO Failure
// Debug.LogFormat("Android and Location not enabled");
// yield break;
//}
#endif
Input.location.Start();
int maxWait = 50;
while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
GPSStatus.text = "Initializing";
yield return new WaitForSeconds(1);
maxWait--;
}
#if UNITY_EDITOR
int editorMaxWait = 15;
while (UnityEngine.Input.location.status == LocationServiceStatus.Stopped && editorMaxWait > 0)
{
yield return new WaitForSecondsRealtime(1);
editorMaxWait--;
}
#endif
if (maxWait < 1)
{
GPSStatus.text = "Time Out";
yield break;
}
if(Input.location.status == LocationServiceStatus.Failed)
{
GPSStatus.text = "Unable to determine Device Location";
yield break;
}
else
{
//Access Granted
GPSStatus.text = "Running";
InvokeRepeating("UpdateGPSData",0.5f,1f);
}
}
private void UpdateGPSData()
{
if(Input.location.status == LocationServiceStatus.Running)
{
//Access granted to GPS
GPSStatus.text = "Running";
latitudeValue.text = Input.location.lastData.latitude.ToString();
longitudeValue.text = Input.location.lastData.longitude.ToString();
altitudeValue.text = Input.location.lastData.altitude.ToString();
horizontalAccuracyValue.text = Input.location.lastData.horizontalAccuracy.ToString();
TimestampValue.text = Input.location.lastData.timestamp.ToString();
}
else if(Input.location.status == LocationServiceStatus.Initializing)
{
//service is initializing
GPSStatus.text = "Initializing";
}
else if (Input.location.status == LocationServiceStatus.Failed)
{
GPSStatus.text = "Failed";
}
else
{
GPSStatus.text = "Stop";
latitudeValue.text = Input.location.lastData.latitude.ToString();
longitudeValue.text = Input.location.lastData.longitude.ToString();
altitudeValue.text = Input.location.lastData.altitude.ToString();
horizontalAccuracyValue.text = Input.location.lastData.horizontalAccuracy.ToString();
TimestampValue.text = Input.location.lastData.timestamp.ToString();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GPSLocation : MonoBehaviour
{
public TextMeshProUGUI GPSStatus;
public TextMeshProUGUI latitudeValue;
public TextMeshProUGUI longitudeValue;
public TextMeshProUGUI altitudeValue;
public TextMeshProUGUI horizontalAccuracyValue;
public TextMeshProUGUI TimestampValue;
// Start is called before the first frame update
void Start()
{
StartCoroutine(GPSLoc());
}
IEnumerator GPSLoc()
{
#if UNITY_EDITOR
// No permission handling needed in Editor
#elif UNITY_ANDROID
if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.FineLocation))
{
UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.FineLocation);
UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.CoarseLocation);
GPSStatus.text = "GRANTED";
}
else
{
GPSStatus.text = "No Access";
Debug.LogFormat("Android Does not have access");
yield break;
}
//if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.Camera))
//{
// UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.Camera);
//}
//else
//{
// Debug.LogFormat("Android Does not have camera access");
// yield break;
//}
// First, check if user has location service enabled
//if (!UnityEngine.Input.location.isEnabledByUser)
//{
// // TODO Failure
// Debug.LogFormat("Android and Location not enabled");
// yield break;
//}
#endif
Input.location.Start();
int maxWait = 50;
while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
GPSStatus.text = "Initializing";
yield return new WaitForSeconds(1);
maxWait--;
}
#if UNITY_EDITOR
int editorMaxWait = 15;
while (UnityEngine.Input.location.status == LocationServiceStatus.Stopped && editorMaxWait > 0)
{
yield return new WaitForSecondsRealtime(1);
editorMaxWait--;
}
#endif
if (maxWait < 1)
{
GPSStatus.text = "Time Out";
yield break;
}
if(Input.location.status == LocationServiceStatus.Failed)
{
GPSStatus.text = "Unable to determine Device Location";
yield break;
}
else
{
//Access Granted
GPSStatus.text = "Running";
InvokeRepeating("UpdateGPSData",0.5f,1f);
}
}
private void UpdateGPSData()
{
if(Input.location.status == LocationServiceStatus.Running)
{
//Access granted to GPS
GPSStatus.text = "Running";
latitudeValue.text = Input.location.lastData.latitude.ToString();
longitudeValue.text = Input.location.lastData.longitude.ToString();
altitudeValue.text = Input.location.lastData.altitude.ToString();
horizontalAccuracyValue.text = Input.location.lastData.horizontalAccuracy.ToString();
TimestampValue.text = Input.location.lastData.timestamp.ToString();
}
else if(Input.location.status == LocationServiceStatus.Initializing)
{
//service is initializing
GPSStatus.text = "Initializing";
}
else if (Input.location.status == LocationServiceStatus.Failed)
{
GPSStatus.text = "Failed";
}
else
{
GPSStatus.text = "Stop";
latitudeValue.text = Input.location.lastData.latitude.ToString();
longitudeValue.text = Input.location.lastData.longitude.ToString();
altitudeValue.text = Input.location.lastData.altitude.ToString();
horizontalAccuracyValue.text = Input.location.lastData.horizontalAccuracy.ToString();
TimestampValue.text = Input.location.lastData.timestamp.ToString();
}
}
}
c#
unity-game-engine
augmented-reality
android-gps
0 Answers
Your Answer