Hello. I want at the start of the game to get the location of the player. I dont want it to be 100% accurate.
I have checked in the player settings the Low Accuracy Location.
Testing on Android
I ask the player about the permission ( Coarse Location) αt the start of the game and i press allow on my mobile.
When the player presses the Play Button, the coroutine starts (from another script).
But after that the Input location enabled by player seems to be false.
I get in the log: Android and Location not enabled.
My code:
private void Start()
{
#if UNITY_EDITOR
// No permission handling needed in Editor
#elif UNITY_ANDROID
if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.CoarseLocation)) {
Debug.LogFormat("Asking for permission on android for Coarse Location.");
UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.CoarseLocation);
}
#elif UNITY_IOS
if (!UnityEngine.Input.location.isEnabledByUser) {
// TODO Failure
Debug.LogFormat("IOS and Location not enabled");
yield break;
}
#endif
}
public IEnumerator LocationCoroutine()
{
#if UNITY_EDITOR
// No permission handling needed in Editor
#elif UNITY_ANDROID
// First, check if user has location service enabled
if (!UnityEngine.Input.location.isEnabledByUser)
{
// TODO Failure
Debug.LogFormat("Android and Location not enabled");
yield break;
}
else
{
Debug.LogFormat("Coarse Permission granted by user.");
}
#endif
// Start service before querying location
UnityEngine.Input.location.Start(1000f);
// Wait until service initializes
int maxWait = 15;
while (UnityEngine.Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSecondsRealtime(1);
maxWait--;
}
// Editor has a bug which doesn't set the service status to Initializing. So extra wait in Editor.
#if UNITY_EDITOR
int editorMaxWait = 15;
while (UnityEngine.Input.location.status == LocationServiceStatus.Stopped && editorMaxWait > 0)
{
yield return new WaitForSecondsRealtime(1);
editorMaxWait--;
}
#endif
// Service didn't initialize in 15 seconds
if (maxWait < 1)
{
// TODO Failure
Debug.LogFormat("Timed out");
yield break;
}
// Connection has failed
if (UnityEngine.Input.location.status != LocationServiceStatus.Running)
{
// TODO Failure
Debug.LogFormat("Unable to determine device location. Failed with status {0}", UnityEngine.Input.location.status);
yield break;
}
else
{
Debug.LogFormat("Location service live. status {0}", UnityEngine.Input.location.status);
// Access granted and location value could be retrieved
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);
var _latitude = UnityEngine.Input.location.lastData.latitude;
var _longitude = UnityEngine.Input.location.lastData.longitude;
// TODO success do something with location
}
// Stop service if there is no need to query location updates continuously
UnityEngine.Input.location.Stop();
}