GPS does not work on Android - only on iOS

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;    
	}
}

@Asta_D did you manage to make gps coordinates change?i am in unity 5.3.5p6 having the same problem . @splitpete Should i update to 5.5.2 in order to fix this problem?

Hello,

My classmates and I ended up upgrading to Unity 5.6 and have seen the GPS work on the Android devices where it was previously not working. The GPS now updates, however when we switch screens it doesn’t restart but that is a bug on my end. I can share my partially working code (since I don’t remember if I changed it) and I’ll probably be working on a fix later this week. I have the code hooked up to a text field on screen in order to see the printouts from the GPS every 5 seconds (it doesn’t actually update every 5 seconds I believe; I don’t think we get that control but I could be wrong).

Edit:
Though I must also mention that I saw it begin to work when I changed my Google API code to UnityWebRequest. This is not related and should not have changed anything so I found that very confusing because that code is in a different script. Maybe it just works on 5.6 now.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class UpdateGPSLocation : MonoBehaviour
{
	public Text debugText;
	public Text distanceDebug;

	LocationInfo myGPSLocation;
	float fiveSecondCounter = 0.0f;

	float currentDistance;

	IEnumerator Start()
	{
		debugText.text += "Starting the GPS Script

";
EventManager.TriggerEvent (GameEvent.GPSInitialized);

		#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;
	}
}

Hi!

I think your sintax is wrong. Line 21 should be:

yield return InitializeGPSServices ();