- Home /
How to consistently check if GPS is enabled
Hi! I hope someone from the community can help us out. We have an ongoing thesis which is a scavenger hunt-type of game in android and it also needs GPS to be activated. Now I already have a code for the initial error if GPS is not enabled yet once the user launches the app:
private IEnumerator StartLocationService()
{
latitude = Input.location.lastData.latitude;
longitude = Input.location.lastData.longitude;
if (!Input.location.isEnabledByUser)
{
Debug.Log ("GPS IS DISABLED");
errorPanel.SetActive (true);
if (PlayerPrefs.GetInt ("LANG") == 0)
{
warningMessage.text = "GPS is not enabled. Please activate GPS and try again.";
retryButton.text = "Retry";
}
if (PlayerPrefs.GetInt ("LANG") == 1)
{
warningMessage.text = "Hindi nakabukas ang iyong GPS. I-activate and iyong GPS at subukang muli.";
retryButton.text = "Ulitin";
}
Debug.Log ("GPS is not enabled");
yield break;
}
Input.location.Start(10, 0.1f);
int maxWait = 20;
while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
if (maxWait <= 0)
{
errorPanel.SetActive (true);
if (PlayerPrefs.GetInt ("LANG") == 0)
{
warningMessage.text = "System has timed out. Please try again";
retryButton.text = "Retry";
}
if (PlayerPrefs.GetInt ("LANG") == 1)
{
warningMessage.text = "Ang system ay nag-time out. Subukang muli.";
retryButton.text = "Ulitin";
}
Debug.Log ("Timed out");
yield break;
}
if (Input.location.status == LocationServiceStatus.Failed)
{
errorPanel.SetActive (true);
if (PlayerPrefs.GetInt ("LANG") == 0)
{
warningMessage.text = "System is unable to determine service location. Please try again";
retryButton.text = "Retry";
}
if (PlayerPrefs.GetInt ("LANG") == 1)
{
warningMessage.text = "Hindi matukoy ng system ang iyong lokasyon. Subukang muli.";
retryButton.text = "Ulitin";
}
Debug.Log ("Unable to determine device location");
yield break;
}
Honestly, only the first if condition is the only function that works and im so confused as to how the GPS functions but one of my main concerns is how can I do it so the panel would always show up if ever the user decides to disable it again after enabling the GPS.
Thanks and cheers!
Answer by cherrybrandy · May 13, 2018 at 06:45 PM
As far as I know Input.location.isEnabledByUser doesn't check if GPS is enabled, it checks if the user has enabled the location service for this app (or the general phone OS setting, that i don't know). I too am looking for a unity API solution to check wether the user has enabled GPS. Seems we have to use a plugin for this.
Answer by ImPHL1 · Jul 14, 2019 at 05:54 PM
hi @eiuolrobles getting latitude and longitude with values of 0 could mean that the GPS is off unless you are exactly in 0,0 (latitude, longitude) location which I don't think that ever gonna happen(really really really low possibility).
Your answer
Follow this Question
Related Questions
Collision Not Working 1 Answer
Is there something wrong with my 'Jump' input axis? 1 Answer
Printing joystick input 0 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Help with if statement 1 Answer