Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Input.location is not working properly!

Discussion in 'Scripting' started by PHL1, Jul 16, 2019.

  1. PHL1

    PHL1

    Joined:
    Jul 25, 2017
    Posts:
    29
    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
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
  3. PHL1

    PHL1

    Joined:
    Jul 25, 2017
    Posts:
    29
    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

    Code (CSharp):
    1.  public void GetUserLocation(){
    2.              //FIRST IM CHACKING FOR PERMISSION IF "true" IT MEANS USER GAVED PERMISSION FOR USING LOCATION INFORMATION
    3.              if(!Input.location.isEnabledByUser){
    4.                  statusTxt.text = "No Permission__ ";
    5.              }
    6.              else
    7.              {
    8.                  statusTxt.text = "Ok Permission__ ";
    9.                  StartCoroutine("GetLatLonUsingGPS");
    10.              }
    11.          }
    12.  
    13.      IEnumerator GetLatLonUsingGPS()
    14.                  {
    15.                      // Start service before querying location
    16.                      Input.location.Start();
    17.                      // Service didn't initialize in 10 seconds
    18.                      int maxWait = 10;
    19.                      while( Input.location.status == LocationServiceStatus.Initializing && maxWait > 0 )
    20.                      {
    21.                          yield return new WaitForSeconds(1);
    22.                          maxWait--;
    23.                      }
    24.                      if( maxWait < 1 )
    25.                      {
    26.                          statusTxt.text = "Failed To Iniyilize in 10 seconds";
    27.                          yield break;
    28.                      }
    29.                      // Connection has failed
    30.                      if( Input.location.status == LocationServiceStatus.Failed )
    31.                      {
    32.                          Debug.Log("Unable to determine device location");
    33.                          statusTxt.text = "Failed To Iniyilize";
    34.                          yield break;
    35.                      }
    36.                      else
    37.                      {
    38.                          // Access granted and location value could be retrieve
    39.                          string longitude = Input.location.lastData.longitude.ToString();
    40.                          string latitude = Input.location.lastData.latitude.ToString();
    41.        
    42.                          AddLocation(double.Parse(latitude), double.Parse(longitude));
    43.                          Debug.Log("lat:"+latitude+"  long:"+longitude);
    44.                          statusTxt.text="" + Input.location.status + "  lat:"+latitude+"  long:"+longitude;
    45.                          if(latitude!="0"){
    46.                              Input.location.Stop();
    47.                              StopCoroutine("Start");
    48.                              statusTxt.text = "" + Input.location.status + "  lat it is:" + latitude ;
    49.                          }
    50.                      }
    51.                      //Stop retrieving location
    52.                      Input.location.Stop();
    53.                  }
    54.                  //
    55.        
    56.              }
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
  5. PHL1

    PHL1

    Joined:
    Jul 25, 2017
    Posts:
    29
    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.
     
  6. PHL1

    PHL1

    Joined:
    Jul 25, 2017
    Posts:
    29
    ok. I'm finally getting the correct location info. this is the code that is perfectly working right now.
    Code (CSharp):
    1. public void GetUserLocation()
    2.         {
    3.             if( !Input.location.isEnabledByUser ) //FIRST IM CHACKING FOR PERMISSION IF "true" IT MEANS USER GAVED PERMISSION FOR USING LOCATION INFORMATION
    4.             {
    5.                 statusTxt.text = "No Permission";
    6.                 Permission.RequestUserPermission(Permission.FineLocation);
    7.             }
    8.             else
    9.             {
    10.                 statusTxt.text = "Ok Permission";
    11.                 StartCoroutine("GetLatLonUsingGPS");
    12.             }
    13.         }
    14.  
    15.         IEnumerator GetLatLonUsingGPS()
    16.         {
    17.             Input.location.Start();
    18.             int maxWait = 5;
    19.             while( Input.location.status == LocationServiceStatus.Initializing && maxWait > 0 )
    20.             {
    21.                 yield return new WaitForSeconds(1);
    22.                 maxWait--;
    23.             }
    24.             if( maxWait < 1 )
    25.             {
    26.                 statusTxt.text = "Failed To Iniyilize in 10 seconds";
    27.                 yield break;
    28.             }
    29.             if( Input.location.status == LocationServiceStatus.Failed )
    30.             {
    31.                 statusTxt.text = "Failed To Initialize";
    32.                 yield break;
    33.             }
    34.             else
    35.             {
    36.                 statusTxt.text ="waiting before getting lat and lon";
    37.                 // yield return new WaitForSeconds(5);
    38.                 // Access granted and location value could be retrieve
    39.                 double longitude = Input.location.lastData.longitude;
    40.                 double latitude = Input.location.lastData.latitude;
    41.  
    42.                 AddLocation(latitude, longitude);
    43.                 statusTxt.text="" + Input.location.status + "  lat:"+latitude+"  long:"+longitude;
    44.             }
    45.             //Stop retrieving location
    46.             Input.location.Stop();
    47.             StopCoroutine("Start");
    48.         }
    all I changed it was deleting that if statement after getting lat,lon , conversion of lat,lon and also adding
    Code (CSharp):
    1. StopCoroutine
    at the end of the method. the sad thing is I'm still not sure what exactly caused that problem
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
  8. PHL1

    PHL1

    Joined:
    Jul 25, 2017
    Posts:
    29
    well, I forgot to mention that I updated unity from 2018.2.11f1 to 2019.1.9f1.
     
  9. mariuslesauter

    mariuslesauter

    Joined:
    Nov 5, 2019
    Posts:
    4
    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?
     
  10. linyanghuan

    linyanghuan

    Joined:
    Jan 11, 2016
    Posts:
    3
    I have same problem, it always Initializing.
     
  11. airevgeny

    airevgeny

    Joined:
    Nov 10, 2019
    Posts:
    2
    Ttry to use old Intput system or both. I had this problem when using new Unity Input system/
     
    banyhong likes this.
  12. frpe1

    frpe1

    Joined:
    May 4, 2020
    Posts:
    10
    I have the same problem using location service using New input system
     
  13. andre-summit

    andre-summit

    Joined:
    Nov 10, 2020
    Posts:
    6
    Same, it never stops initializing. Unity 2020.2.1f1, tested on Samsung S20 with Android 10.
     
  14. Cuicui_Studios

    Cuicui_Studios

    Joined:
    May 3, 2014
    Posts:
    72
    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.
     
    Last edited: Nov 24, 2021
    Fenikkel likes this.
  15. craigoproducto

    craigoproducto

    Joined:
    Jan 25, 2022
    Posts:
    1
    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.
     
  16. emirsoylu03

    emirsoylu03

    Joined:
    Jul 14, 2019
    Posts:
    3
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Android;
    5. using UnityEngine.UI;
    6.  
    7. public class GetLocations : MonoBehaviour
    8. {
    9.     public Text statusTxt;
    10.  
    11.     void Update()
    12.     {
    13.         GetUserLocation();
    14.     }
    15.  
    16.  
    17.     public void GetUserLocation()
    18.     {
    19.         if (!Input.location.isEnabledByUser) //FIRST IM CHACKING FOR PERMISSION IF "true" IT MEANS USER GAVED PERMISSION FOR USING LOCATION INFORMATION
    20.         {
    21.             statusTxt.text = "No Permission";
    22.             Permission.RequestUserPermission(Permission.FineLocation);
    23.         }
    24.         else
    25.         {
    26.             statusTxt.text = "Permission Granted";
    27.             StartCoroutine(GetLatLonUsingGPS());
    28.         }
    29.     }
    30.  
    31.     IEnumerator GetLatLonUsingGPS()
    32.     {
    33.         Input.location.Start();
    34.         int maxWait = 5;
    35.         while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
    36.         {
    37.             yield return new WaitForSeconds(1);
    38.             maxWait--;
    39.         }
    40.  
    41.         statusTxt.text = "waiting before getting lat and lon";
    42.        
    43.         // Access granted and location value could be retrieve
    44.         double longitude = Input.location.lastData.longitude;
    45.         double latitude = Input.location.lastData.latitude;
    46.        
    47.         //AddLocation(latitude, longitude);
    48.         statusTxt.text = "" + Input.location.status + "  lat:" + latitude + "  long:" + longitude;
    49.  
    50.         //Stop retrieving location
    51.         Input.location.Stop();
    52.         StopCoroutine("Start");
    53.     }
    54. }
    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.
     
  17. abdoxdooo

    abdoxdooo

    Joined:
    Jan 30, 2019
    Posts:
    1
    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
     
  18. frpe1

    frpe1

    Joined:
    May 4, 2020
    Posts:
    10
    Still have the same issue with Location its stuck in Initializing. Having Samsung Galaxy S10e. Even when update the OS to android level 11.
     
  19. frpe1

    frpe1

    Joined:
    May 4, 2020
    Posts:
    10

    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 ?
     
  20. nick-morhun

    nick-morhun

    Joined:
    May 12, 2015
    Posts:
    51
    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.