Hello,
I am using Unity for my project for its ability to compile to both Android and iOS. However, the GPS does not seem to work for Android. From searching these forums it seems like everyone else has also been having issues with GPS updating on Android? How should I fix this problem? If it is a bug, how long will it take for Unity to fix this issue with Android devices?
My code currently updates the GPS location every 5 seconds. It works on iOS and updates as I walk, but for Android no matter how far I walk I keep getting the same coordinate, the initial coordinate, it never updates. I tested on two Android devices, one was Android 7.1.2
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UpdateGPSLocation : MonoBehaviour
{
public Text debugText;
public Text distanceDebug;
LocationInfo myGPSLocation;
float fiveSecondCounter = 0.0f;
IEnumerator Start()
{
debugText.text += "Starting the GPS Script
";
#if UNITY_ANROID
Input.compass.enabled = true;
#endif
return InitializeGPSServices ();
}
IEnumerator InitializeGPSServices()
{
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser) {
debugText.text += "GPS disabled by user
";
yield break;
}
// Start service before querying location
Input.location.Start(0.1f, 0.1f);
// Wait until service initializes
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1)
{
debugText.text += "Timed out
";
yield break;
}
}
void Update()
{
fiveSecondCounter += Time.deltaTime;
if (fiveSecondCounter > 5.0)
{
UpdateGPS ();
fiveSecondCounter = 0.0f;
}
}
void UpdateGPS()
{
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
debugText.text += "Unable to determine device location
";
Input.location.Stop();
Start ();
}
else
{
debugText.text += getUpdatedGPSstring();
}
}
string getUpdatedGPSstring()
{
myGPSLocation = Input.location.lastData;
return "Location: " + myGPSLocation.latitude + " " + myGPSLocation.longitude + " " + myGPSLocation.altitude + " " + myGPSLocation.horizontalAccuracy + " " + myGPSLocation.timestamp + "
";
}
public Vector2 GetCoordinate()
{
return new Vector2 (myGPSLocation.latitude, myGPSLocation.longitude);
}
// For the IShowHideListener from the HomeUIObject
public void OnShow()
{
InitializeGPSServices ();
}
public void OnHide()
{
// Stop service if there is no need to query location updates continuously
Input.location.Stop ();
debugText.text = "";
// Make sure it immediately updates when the screen shows again
fiveSecondCounter = 5.1f;
}
}