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:
Unity - Scripting API: LocationService.Start 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’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.

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…

Hey. Did anyone find a solution to the above. I have all the APIs but I’m still stuck at enabling location service.

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.

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 Unity - Manual: Request runtime permissions to check if the user has given permission and ask for it in case they haven’t. Hope this helps.

Here is the solution,

if you have two scenes in your app, then attach the location service script at the splash scene also, and remember: omit the first condition from your app

if (!Input.location.isEnabledByUser)
{

         print("Location service is not enabled!");
         yield break;

}
comment the avobe mentioned section. NOW YOU ARE GOOD TO GO.

also remember: attach the location service script at the splash scene and also at the scene where you want to fetch lattitude and longitude okay.

If anyone is still being bugged with this issue (like i was), here you have a script that i tried to make today. Surprisingly after a year and a half of issues this actually works.

using UnityEngine;
using UnityEngine.UI;

public class gps : MonoBehaviour
{
    public Text lat;
    public Text lon;


    void Update()
    {
        if (!Input.location.isEnabledByUser)
        {
            Debug.LogError("Location not enabled");
        }
        else if (Input.location.status != LocationServiceStatus.Running) {
            Input.location.Start(1, 0.1f);
        }
        Debug.LogError(Input.location.status);
        
        lat.text = Input.location.lastData.latitude.ToString();
        print(Input.location.lastData.latitude);
        lon.text = Input.location.lastData.longitude.ToString();
        print(Input.location.lastData.longitude);
    }
}

Hope it helps someone :slight_smile:

You want to open location service android panel and get callback of GPS through below plugin -

Thanks, Deba124 - that did the trick!

// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
print(“Location service is not enabled!”);
yield break;
yiled break, is not included in {}

Try restarting your android device. It will work.

I was getting this problem LocationService is not enabled, I had allowed permissions and tried everything from official Unity Documentation, but nothing was working. Later when I restarted my Android Device, it was working properly.