Input.location is not working properly!

hi. I already post a question in unity answers but I got nothing. the problem is I get the correct latitude and longitude only when GPS is on before I start the game. and even when game is running and i turn off the location and turn it on again(after like 10 seconds) the latitude and longitude I get is always zero unless I close the game and run it again( of curse with GPS ON, also if I turn on location when game is already runing i will get 0,0(lat,lon) no matter what I do!.
i tested te app on android 7.0
thanks

There is a lot of information that you should provide in order to receive help more quickly.

For instance, tell us what you've tried? What's the code you're using?

I never used that service, but one of the first search results shows in a very trivial example how you can manage the location service at runtime.

Due to the lack of information, I'd say check the initialization status and see what it is when it returns zeroes. Also try to start it explicitly. Perhaps the service is auto-enabled when it is enabled at startup, and requires a manual start when it's enabled at runtime?

If you've tried that already, then you'll need to post more information. Otherwise this is going to be lots of wild guessing.

ok, this is my code its like the code in the link you sent. I tried this code but the result is like I said before. please let me know if you need more info.tnx

 public void GetUserLocation(){
             //FIRST IM CHACKING FOR PERMISSION IF "true" IT MEANS USER GAVED PERMISSION FOR USING LOCATION INFORMATION
             if(!Input.location.isEnabledByUser){
                 statusTxt.text = "No Permission__ ";
             }
             else
             {
                 statusTxt.text = "Ok Permission__ ";
                 StartCoroutine("GetLatLonUsingGPS");
             }
         }

     IEnumerator GetLatLonUsingGPS()
                 {
                     // Start service before querying location
                     Input.location.Start();
                     // Service didn't initialize in 10 seconds
                     int maxWait = 10;
                     while( Input.location.status == LocationServiceStatus.Initializing && maxWait > 0 )
                     {
                         yield return new WaitForSeconds(1);
                         maxWait--;
                     }
                     if( maxWait < 1 )
                     {
                         statusTxt.text = "Failed To Iniyilize in 10 seconds";
                         yield break;
                     }
                     // Connection has failed
                     if( Input.location.status == LocationServiceStatus.Failed )
                     {
                         Debug.Log("Unable to determine device location");
                         statusTxt.text = "Failed To Iniyilize";
                         yield break;
                     }
                     else
                     {
                         // Access granted and location value could be retrieve
                         string longitude = Input.location.lastData.longitude.ToString();
                         string latitude = Input.location.lastData.latitude.ToString();

                         AddLocation(double.Parse(latitude), double.Parse(longitude));
                         Debug.Log("lat:"+latitude+"  long:"+longitude);
                         statusTxt.text="" + Input.location.status + "  lat:"+latitude+"  long:"+longitude;
                         if(latitude!="0"){
                             Input.location.Stop();
                             StopCoroutine("Start");
                             statusTxt.text = "" + Input.location.status + "  lat it is:" + latitude ;
                         }
                     }
                     //Stop retrieving location
                     Input.location.Stop();
                 }
                 //

             }

I would suggest you try leaving location services turned on and see if the location changes to something reasonable after a minute or so. I know that Unity's example doesn't do this, and you probably shouldn't have to, but it seems worth testing out. I could imagine that there's a "warm up" period before the data becomes reliable when you are starting it "cold" (without having previously had permission).

Also, I notice you are taking the latitude and longitude, converting them to strings, and then parsing those strings into doubles. That's rather wasteful.

actually, I tested that before but I'm getting the same result even after like 10 seconds. there's might be a method related to location that I'm not aware of or maybe this is a unity bug!! but I'm gonna test it again with a longer period before I get lat and lon.
yeah, you right about converting.

ok. I'm finally getting the correct location info. this is the code that is perfectly working right now.

public void GetUserLocation()
        {
            if( !Input.location.isEnabledByUser ) //FIRST IM CHACKING FOR PERMISSION IF "true" IT MEANS USER GAVED PERMISSION FOR USING LOCATION INFORMATION
            {
                statusTxt.text = "No Permission";
                Permission.RequestUserPermission(Permission.FineLocation);
            }
            else
            {
                statusTxt.text = "Ok Permission";
                StartCoroutine("GetLatLonUsingGPS");
            }
        }

        IEnumerator GetLatLonUsingGPS()
        {
            Input.location.Start();
            int maxWait = 5;
            while( Input.location.status == LocationServiceStatus.Initializing && maxWait > 0 )
            {
                yield return new WaitForSeconds(1);
                maxWait--;
            }
            if( maxWait < 1 )
            {
                statusTxt.text = "Failed To Iniyilize in 10 seconds";
                yield break;
            }
            if( Input.location.status == LocationServiceStatus.Failed )
            {
                statusTxt.text = "Failed To Initialize";
                yield break;
            }
            else
            {
                statusTxt.text ="waiting before getting lat and lon";
                // yield return new WaitForSeconds(5);
                // Access granted and location value could be retrieve
                double longitude = Input.location.lastData.longitude;
                double latitude = Input.location.lastData.latitude;

                AddLocation(latitude, longitude);
                statusTxt.text="" + Input.location.status + "  lat:"+latitude+"  long:"+longitude;
            }
            //Stop retrieving location
            Input.location.Stop();
            StopCoroutine("Start");
        }

all I changed it was deleting that if statement after getting lat,lon , conversion of lat,lon and also adding StopCoroutine at the end of the method. the sad thing is I'm still not sure what exactly caused that problem

Well, changing code that only executes after you collect your data obviously isn't going to change the data that you already collected, so presumably something else must be going on.

well, I forgot to mention that I updated unity from 2018.2.11f1 to 2019.1.9f1.

I am using the Unity example Code for getting the Input.service.location of a Nexus 5x and the GPS doesn't work at all (gets a time out every time). I am using Unity 2019.4.13f1

Does anybody know a solution to this?


I have same problem, it always Initializing.

Ttry to use old Intput system or both. I had this problem when using new Unity Input system/

1 Like

I have the same problem using location service using New input system

Same, it never stops initializing. Unity 2020.2.1f1, tested on Samsung S20 with Android 10.

November 23, 2021. Input.location.isEnabledByUser equals false no matter what Input system we use (old, new or both) and that the user has accepted the FineLocation permission. Also, never stops initialising. So frustrating.

1 Like

i was having the same issue just with location data returning stopped and showing all 0s.

my issue was because my phone was in power saving mode. despite location services being turned on it looks like unity doesn't attempt to turn location services on when this is enabled.

thought i would post this here just in case anyone has the same issue.


For some reason, This code is not working and statusTxt is stuck on the "Failed To Iniyilize in 10 seconds" text. However, this problem is easy to solve! Just make it more straightforward! Remove all the if's in GetLatLonUsingGPS() method. My code was like that:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.UI;

public class GetLocations : MonoBehaviour
{
    public Text statusTxt;

    void Update()
    {
        GetUserLocation();
    }


    public void GetUserLocation()
    {
        if (!Input.location.isEnabledByUser) //FIRST IM CHACKING FOR PERMISSION IF "true" IT MEANS USER GAVED PERMISSION FOR USING LOCATION INFORMATION
        {
            statusTxt.text = "No Permission";
            Permission.RequestUserPermission(Permission.FineLocation);
        }
        else
        {
            statusTxt.text = "Permission Granted";
            StartCoroutine(GetLatLonUsingGPS());
        }
    }

    IEnumerator GetLatLonUsingGPS()
    {
        Input.location.Start();
        int maxWait = 5;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        statusTxt.text = "waiting before getting lat and lon";

        // Access granted and location value could be retrieve
        double longitude = Input.location.lastData.longitude;
        double latitude = Input.location.lastData.latitude;

        //AddLocation(latitude, longitude);
        statusTxt.text = "" + Input.location.status + "  lat:" + latitude + "  long:" + longitude;

        //Stop retrieving location
        Input.location.Stop();
        StopCoroutine("Start");
    }
}

There is only one problem in this code; don't call the GetUserLocation() method on the Update() method without any control bool or timer. Otherwise, StatusTxt will continuously change. For example, you can call it every 5 seconds.

i removed GetUserLocation() from Update method and i made it called when pressing a button but give me
(Stopped: lat:0 long:0) where is the problem

Still have the same issue with Location its stuck in Initializing. Having Samsung Galaxy S10e. Even when update the OS to android level 11.

That works!, but why ? not seeing the logic here why Unity’s one “example” didn’t work at all. Its basically almost the same code, except you updating all the time in “Update” functions. Not understanding this. Couldn’t this be done in another way ?

I had to set Location in the app settings to "Allow only when using the app" to get Permission.HasUserAuthorizedPermission(Permission.FineLocation) set to true. But Input.location.isEnabledByUser was still false because my global Android location setting was OFF.
If I set the setting to "Ask every time", there is no popup, just denied.