The main objective of this post is to help you detect GPS (Real world location) of Mobile Device in unity.
This will be useful if you wish to add Google Maps and similar service to your unity project.
1. Create script.
Create a script by right clicking in Project Window and name it GPS or any.
2. Open to edit script.
3. Add UnityEngine.Android.
using UnityEngine.Android;
4. Check if player has given location access if not then call request.
if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
{
Permission.RequestUserPermission(Permission.FineLocation);
}
5. Above one will work for android, and for iOS we need to provide location usage description.
You can do this in 2 different ways.
5.1 Do it from script.
5.1.1 Add UnityEditor.
using UnityEditor;
5.1.2 Provide location usage description.
PlayerSettings.iOS.locationUsageDescription = "Details to use location";
5.2 Or do it from Editor.
5.2.1 Go to File -> BuildSettings
5.2.2 iOS -> Player Settings -> Other Settings
5.2.3 Under “Configuration” -> “Location Usage Description” provide your description.
6. Now we will create a coroutine called “StartLocationService”
private IEnumerator StartLocationService()
{}
7. Now if user has allowed to use location then we will start location service.
if (Input.location.isEnabledByUser)
{
Input.location.Start();
}
8. Then we will check is status is initializing or not and wait for it to either success or failure.
while (Input.location.status == LocationServiceStatus.Initializing)
{
yield return new WaitForSeconds(1);
}
9. Now if status has changed then this loop will be exited and we can check for fail result and break coroutine.
if (Input.location.status == LocationServiceStatus.Failed)
{
Debug.Log("Unable to determine device location");
yield break;
}
10. After this if status isn’t fail then we can fetch out location values.
Debug.Log ("Latitude : " + Input.location.lastData.latitude);
Debug.Log ("Longitude : " + Input.location.lastData.longitude);
Debug.Log("Altitude : " + Input.location.lastData.altitude);
11. So your final start method and coroutine will look like.
private void Start()
{
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission (Permission.FineLocation))
{
Permission.RequestUserPermission (Permission.FineLocation);
}
#elif UNITY_IOS
PlayerSettings.iOS.locationUsageDescription = "Details to use location";
#endif
StartCoroutine(StartLocationService());
}
private IEnumerator StartLocationService()
{
if (!Input.location.isEnabledByUser)
{
Debug.Log("User has not enabled location");
yield break;
}
Input.location.Start();
while(Input.location.status == LocationServiceStatus.Initializing)
{
yield return new WaitForSeconds(1);
}
if (Input.location.status == LocationServiceStatus.Failed)
{
Debug.Log("Unable to determine device location");
yield break;
}
Debug.Log ("Latitude : " + Input.location.lastData.latitude);
Debug.Log ("Longitude : " + Input.location.lastData.longitude);
Debug.Log("Altitude : " + Input.location.lastData.altitude);
}
Now you can pass these values to any map sdk like Google Street map or any.
I hope you will find this blogpost very useful while detecting GPS location of your mobile device.
Let me know in the comment section below if you have any questions regarding this post or any other topic of Unity, I will try to reply ASAP.
Great content! Super high-quality! Keep it up! 🙂
Debug.Log (“Latitude : ” + Input.location.lastData.latitude);
Debug.Log (“Longitude : ” + Input.location.lastData.longitude);
Debug.Log(“Altitude : ” + Input.location.lastData.altitude);
Will coordinates be printed? 2-letter ISO country codes? Is there a documentation for the list of possibilities? And does this only work on the phone, but not on the Unity Editor? Thanks.
Hey Ian,
It will work on phone as well as in Unity Editor.
And you can check that by just running it in play mode.
not working in android 10.
Works Fine, You need to remove conflicts from your project.
Hello Milan, the code works very fine and thanks for the code. But, this works only when the user has already has his gps location turned-on in his phone. If the user doesn’t have the gps turned on, the code does not work. do you have any solution to prompt the native Android’s TURN ON GPS dialog box to enable the GPS in the phone through code? that would be of great help! Thanks in advance
Thanks, Krishna,
There is a reference available in android native documents on the https://developer.android.com.