- Home /
need help with Input.location!
hi. is there something about Input.location that I don't know? because it gives me the right latitude and longitude only when I turn on location of my phone before running the game(exactly before)!! and lets say I start the game with location on, if I turn off location and turn it on again I will get latitude and longitude zero forever unless I close the game and start it again!! thnaks @Hellium
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();
}
//
}
Provide the script that is looking for this input; I personally have never used this function so my help wouldn't be first hand and guess work.
How are you calling the coroutine? Do you have a method that checks your location again after you log off?
As stated, I have never used this function but if it does work until you turn it off, then maybe you never call this method again after you turn it back on.
1- i added the other method that calls this coroutine. 2 - no it's just this method
I need to get the location whenever the player wants it so this method will be called multiple times. but that's not the only problem as I said in the question: if the player starts the game with a GPS off and turn it on meanwhile the game is running the lat and lon will stay zero and obviously wrong data.
Your answer
![](https://koobas.hobune.stream/wayback/20220612204924im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to convert Touch Input To Mouse Input C# unity? 1 Answer
Trying to find what player inputed in Text Input field 1 Answer