Unity5 and Unity Remote 5 will not work with GPS on my Android lolipop 6.01 phone
So I have a Android Lollipop 6.0.1. I have gone through the tutorials to make it work with accelerometer, gyro and compass. All of which work. However it always seems to think that the GPS on my phone is not enabled even though it is.
This is the same after I compile it it and install it on the phone and if a try to run it through Unity Remote 5.
Am I missing an extra setting? My GPS Location is turned on on my phone and it works with other phone apps. Just not the Unity ones that I'm trying to create. I'm just trying to get simple latitude and longitude data
The code I am using is below. I only get to the point in the code !Input.location.isEnabledByUser and it kicks me out with the Debug.Log "user has not enabled gps"
Thanks All, hope you can help.
using UnityEngine;
using System.Collections;
using System;
public class GPS : MonoBehaviour {
public static GPS Instance { set; get; }
public float latitude;
public float longitude;
private void Start()
{
Instance = this;
DontDestroyOnLoad(gameObject);
StartCoroutine(StartLocationService());
}
private IEnumerator StartLocationService()
{
if (!Input.location.isEnabledByUser)
{
Debug.Log("user has not enabled gps");
yield break;
}
Input.location.Start();
int maxWait = 20;
while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
if (maxWait <= 0)
{
Debug.Log("Timed Out");
yield break;
}
if(Input.location.status == LocationServiceStatus.Failed)
{
Debug.Log("Unable to dtermine device location");
yield break;
}
latitude = Input.location.lastData.latitude;
longitude = Input.location.lastData.longitude;
yield break;
}
}
Answer by juldaani · Mar 23, 2017 at 01:48 PM
I had exactly the same problem. The solution which worked for me was to add some delay into StartLocationService(). I think the problem is that Unity Remote and Unity Editor require some time to establish a connection. If you try to start the location service before the connection between remote and editor is established you will fail. This will happen if you try to start location service in the start().
See my solution below:
private IEnumerator StartLocationService(float desiredAccuracyInMeters, float updateDistanceInMeters)
{
// Wait until the editor and unity remote are connected before starting a location service
if (isUnityRemote)
{
yield return new WaitForSeconds(5);
}
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
{
Debug.Log("No locations enabled in the device");
yield break;
}
// Start service before querying location
Input.location.Start();
if (isUnityRemote)
{
yield return new WaitForSeconds(5);
}
// 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)
{
Debug.Log("Service didn't initialize in 20 seconds");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
Debug.Log("Unable to determine device location");
yield break;
}
// Access granted and location value could be retrieved
else
{
float lat = Input.location.lastData.latitude;
float lon = Input.location.lastData.longitude;
}
}
I added 2 seconds delay in the beginning of the StartLocationService() and directly after the Input.location.Start(). I also made a global boolean isUnityRemote to be able to disable these delays if I want. You could also use "Input.location.status == LocationServiceStatus.Running" in the while loop instead of this if you want to be more elegant.
Thanks for the tip - adding a delay on start-up seems to have fixed it for me!
Thank you very much! The delays also fixed my issues with the Remote 5 app using GPS data.
Your answer
Follow this Question
Related Questions
How can I get 5 decimal places of accuracy for latitude and longitude from locationservice? 3 Answers
GPS power consumption 0 Answers
how to place 2d/3d objects in real world based on location and altitude... 0 Answers
Trying to Build GPS Tracking Based App, Help 0 Answers
Augmented Reality Navigation 0 Answers