Getting GPS on iOS or Android
Hello,
I have been stuck lately trying to get the users gps coordinates on device. I have put some debug logs to see what is going on and it looks like the Input.location.status never switches states from LocationServiceStatus.Initializing. Is this potentially a bug in unity? I am using the laters unity 5 version (pro license). Here is my code:
public class LocationServiceManager : MonoBehaviour
{
private float latitude;
private float longitude;
private float elapsedTime;
private bool isEnabledByUser;
private bool hasResolvedLocation;
public static LocationServiceManager Create()
{
GameObject go = new GameObject();
return go.AddComponent<LocationServiceManager>();
}
public bool IsLocationAvailable
{
get {
return (isEnabledByUser && hasResolvedLocation);
}
}
public Vector2 Coordinates
{
get { return new Vector2(latitude, longitude); }
}
public override void Awake()
{
base.Awake();
latitude = 0.0f;
longitude = 0.0f;
elapsedTime = 0.0f;
isEnabledByUser = false;
hasResolvedLocation = false;
//Self Organization
gameObject.name = "location manager";
GameObject.DontDestroyOnLoad(gameObject);
Debug.LogError("LocationServiceUtil::Start()");
isEnabledByUser = Input.location.isEnabledByUser;
if(isEnabledByUser)
{
Debug.LogError("LocationServiceUtil::Start() location is enabled by user");
// Start service before querying location
Input.location.Start(1000.0f, 1000.0f);
Input.compass.enabled = true;
}
}
private void Update ()
{
elapsedTime -= Time.deltaTime;
if(!isEnabledByUser || hasResolvedLocation || elapsedTime > 0.0f)
{
return;
}
// Wait until service initializes
while (Input.location.status == LocationServiceStatus.Initializing)
{
Debug.LogError("Location initializing ... try again ..");
elapsedTime = 1.0f;
return;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
Debug.LogError("LocationServiceUtil::Start() Location failed");
}
else
{
latitude = Input.location.lastData.latitude;
longitude = Input.location.lastData.longitude;
Debug.LogError("LocationServiceUtil::Start() Location found latitude: " + latitude + " longitude: " + longitude);
}
// Stop service if there is no need to query location updates continuously
hasResolvedLocation = true;
Input.location.Stop();
}
}
Any ideas?
Comment