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\n";
#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\n";
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\n";
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\n";
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 + "\n";
}
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;
}
}
Not sure which version of Unity you're on but there was a known bug in 5.5.1 that was causing a problem on Android for this issue. I upgraded to 5.5.2 and it had been fixed.
Here's a link to the issue tracker: https://issuetracker.unity3d.com/issues/android-location-app-crashes-due-to-error-jstring-has-wrong-type-android-dot-location-dot-location
I have exactly the same problem. Im using Unity 5.6.0f3 and no matter what i do it only gets the gps coordinates once and then never again. I even followed three diffrent tutorials and every time the same ... the gps coordinates dosen't change. even if i walk have a mile. $$anonymous$$y Device is an Galaxy S6 Edge with Android 6.0.1 on it.
Answer by zapantis88 · May 03, 2017 at 02:26 AM
@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?
The issue tracker I referenced has the earliest version of the bug at 5.5.0f1. That doesn't mean it wasn't present prior to that. You could look for other issue trackers which are more specific to your problem. I'm currently using the latest version 5.6.0 and I don't have this issue.
Answer by Kaikat · May 09, 2017 at 11:08 PM
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\n";
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\n";
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\n";
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\n";
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 + "\n";
}
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;
}
}
Answer by Nimdanet · Jun 25, 2019 at 09:48 PM
Hi!
I think your sintax is wrong. Line 21 should be:
yield return InitializeGPSServices ();
Your answer
Follow this Question
Related Questions
Black Artifacts on some android devices after upgrading from 2017 to 2018 2 Answers
Location Services not starting when system GPS is enabled. 0 Answers
Location service not updating the input value 0 Answers
GPS power consumption 0 Answers
how to place 2d/3d objects in real world based on location and altitude... 0 Answers