- Home /
Unity location service not working
I am trying to make a location based app and i cant seem to get it to work. I used the example code here: https://docs.unity3d.com/ScriptReference/LocationService.Start.html but the code does not work, after a while i discovered that the problem was that location service was not enabled. How do i fix this? My code:
PS i am using android
using UnityEngine;
using System.Collections;
public class Location : MonoBehaviour
{
IEnumerator Start()
{
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
print("Location service is not enabled!");
yield break;
// Start service before querying location
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)
{
print("Timed out");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
print("Unable to determine device location");
yield break;
}
else
{
// Access granted and location value could be retrieved
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
}
// Stop service if there is no need to query location updates continuously
Input.location.Stop();
}
}
I have the exact same problem. I dont think its the code though. $$anonymous$$aybe you have to enable something in unity build settings or maybe its eve an error in the phone. (Im using samsung galaxy S5)
Answer by loughrank · Feb 23, 2018 at 03:59 PM
I'm also using this code sample from the Unity documentation, however, it always returns a zero for all values when testing on iOS, Android, via Remote or directly in the Editor.
Answer by Kettch · Mar 01, 2017 at 07:16 AM
I had the same problem, then i thought about checking Android Studio settings, i was only running Android 7.1 Components (API Level 25).
My unity remote works on a S5 too @mihoci (API Level19)
i'm Downloading all sub-versions to 4.4 (just to be sure)
still not working....
Answer by stephenmutua · Mar 07, 2018 at 01:53 PM
Hey. Did anyone find a solution to the above. I have all the APIs but I'm still stuck at enabling location service.
Answer by russpatterson · May 13, 2018 at 04:11 PM
I found the same problem. Input.location.isEnabledByUser is flaky in Unity version 2017.3.0f3 it always starts out saying false and sometimes changes it's mind. I found that if the status is already running, e.g. "Input.location.status == LocationServiceStatus.Running" then you can just start using the gps data. It seems like writing a plugin to access the gps chips is the way to go.
Answer by JohanLoredo · Feb 22, 2019 at 03:59 PM
Hello, I had a similar problem using the same sample code; in my case, it turned out that my app didn't request permission upon initialization, so there were two ways to solve it: 1) To manually enable the permission from your app configuration on your phone. 2) Use the information on this link https://docs.unity3d.com/Manual/android-RequestingPermissions.html to check if the user has given permission and ask for it in case they haven't. Hope this helps.