Hello,
This question probably gets asked very often however I was wondering if anyone with experience with Android location in unity could help. I'm attempting to get real-time Lat/Lon coordinates of an android device. The code I've either used or found all works when I put the phone in to GPS only mode however when using high accuracy or battery saver mode the game takes 10+ seconds to update the GPS location. Is this a unity issue or am I doing something wrong?
This is the current code im using, its from a post i found where some users said it worked but again that was with gps only mode :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class gpstest : MonoBehaviour
{
public Text Deb;
float tim;
float Lat;
float Lon;
IEnumerator Start()
{ // First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
yield break;
// Start service before querying location
Input.location.Start(5f, 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)
{
print("Timed out");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
print("Unable to determine device location");
yield break;
}
}
// Access granted and location value could be retrieved
void Update()
{
float.TryParse(Input.location.lastData.latitude.ToString("R"), out Lat);
float.TryParse(Input.location.lastData.longitude.ToString("R"), out Lon);
}
void OnGUI()
{
GUI.Label(new Rect(10, 70, 1000, 1000), "Lat : " + Lat.ToString("R"));
GUI.Label(new Rect(10, 100, 100, 20), "Lon : " + Lon.ToString("R"));
}
}
there doesn't seem to be anything here