- Home /
How to import GPS location coordinates from Android device?
Hello, I tried to get latitude and longitude coordinates from 'Google Nexus One' and I failed.
I used the LocationInfo struct, but I think there is something missing. I wrote an Update function attached to 'GUI Text' like this:
void Update() { float lat, lon;
LocationInfo li = new LocationInfo();
lat = li.latitude;
lon = li.longitude;
guiText.text="LAT= "+ lat +"\nLON= " + lon;
}
Always returns 0.
Any idea? Thanks in advance,
Joel
Answer by Joshua Falkner · May 31, 2011 at 11:31 PM
The answer to this is actually in the last place you'd expect. You have to first initialize the iPhoneSettings.StartLocationServiceUpdates() (Yes, I know you're on android. You have to use iPhoneSettings anyway) and then when the services is up and running update your loc variable with loc = iPhoneInput.lastLocation;
Read more about it here: http://unity3d.com/support/documentation/ScriptReference/iPhoneSettings.StartLocationServiceUpdates.html
Answer by Maarten · Dec 05, 2010 at 08:42 PM
You are just creating an new LocationInfo object, which will always return 0. You should call a function to retrieve the values from your device.
I do not know which function you should call to retrieve them.
And that function is from unity libraries or android sdk?
Anyone has tested GPS with Android?
I think you have to use the functions in the Android SD$$anonymous$$, and using a Plugin for Unity to be able to use them.
For more information on plugins, please see the url below: http://unity3d.com/support/documentation/$$anonymous$$anual/Plugins.html#AndroidPlugins
Answer by MESSAOUDI · Jul 19, 2013 at 09:59 AM
Hello everyone, this is surely easy to arrange as a problem, which aims to use using methods use "location" for Unity 3D?
Thank you
Answer by Imenbm · Mar 17, 2017 at 04:15 PM
you have to add this :
void Start()
{
Input.location.Start();
}
void update()
{
StartCoroutine(UpdateLocation()) ; }
IEnumerator UpdateLocation() { Input.location.Start();
// 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)
{
Text singleText = mytext1.GetComponentInChildren<Text>();
singleText.text = "Timed out";
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
Text singleText = mytext1.GetComponentInChildren<Text>();
singleText.text = "Unable to determine device location";
}
else
{
float lat = Input.location.lastData.latitude;
float lon = Input.location.lastData.longitude;
}}