Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

GPS Input.Location accuracy question

Discussion in 'Android' started by MatD, Apr 17, 2012.

  1. MatD


    Apr 17, 2012
    Hello to you all,

    I have a technical question concerning the GPS functionality in Unity under Android
    I'm building and running following code for Android 2.2 (SDK) to retrieve GPS data.
    Pretty basic code, but the accuracy of the retrieved Input.location Longitude and Latitude is so bad (sometimes error of about 1-2km) that it's almost impossible for example to compute a speed or to show a Google Maps image tile of my current GPS position. I have tested it with several values for Input.location.Start() but without a notable improving.

    I have a strange feeling that the GPS Position is not provided by the inbuilt-GPS component (of the Android device) but caught from the mobile carrier antenna position or over Wi-FI (WPS) explaining why I have positions that are sometimes 1-2kms far away from the real position. When I'm abroad (and being unable to use the data-connection) and launching my app, I won’t get any GPS data which leads me to think that.
    Has someone experienced the same issue? Here’s my code.

    I’ve used InvokeRepeating() for the sake of performance ; placing the same code in Update() has the same effect.

    Code (csharp):
    2. #define ANDROID
    3. using UnityEngine;
    4. using System.Collections;
    5. using GeoUtility;
    6. using GeoUtility.GeoSystem;
    7. using System.Timers;
    9. public class GPSManager : MonoBehaviour {
    11.     bool gpsInit = false;
    12.     LocationInfo currentGPSPosition;
    14.     void Start () {
    16. #if PC
    17.         Debug.Log("On PC / Don't have GPS");
    18. #elif !PC
    20.         //Starting the Location service before querying location
    21.         Input.location.Start(0.5f); // Accuracy of 0.5 m
    23.         int wait = 1000; // Per default
    25.         // Checks if the GPS is enabled by the user (-> Allow location )
    26.         if(Input.location.isEnabledByUser)
    27.         {
    28.             while(Input.location.status == LocationServiceStatus.Initializing  wait>0)
    29.             {
    30.                 wait--;
    31.             }
    34.             if (Input.location.status == LocationServiceStatus.Failed) {
    36.             }
    37.             else {
    38.                 gpsInit = true;
    39.                 // We start the timer to check each tick (every 3 sec) the current gps position
    40.                 InvokeRepeating("RetrieveGPSData", 0, 3);
    41.             }
    42.         }
    43.         else
    44.         {
    45.             GameObject.Find("gps_debug_text").guiText.text = "GPS not available";
    46.         }
    47.         #endif 
    48.     }
    50.     void RetrieveGPSData()
    51.     {
    52.         currentGPSPosition = Input.location.lastData;
    53.         string gpsString = "::" + currentGPSPosition.latitude + "//" + currentGPSPosition.longitude;
    54.         GameObject.Find("gps_debug_text").guiText.text = gpsString;
    55.     }
    Thank you for your help and tech advices!
  2. Tseng


    Nov 29, 2010
    Well, first off you have set the GPS accuracy, but not the update distance (default 10m). So it won't be updated unless the user moves 10m from the last location.

    The other thing is, you will unlikely get a high accuracy (less than 20 meters) while inside of buildings. To get a good accuracy you need to be outside and have as less obstacles as possible (i.e. accuracy in a field is much better than when you are surrounded by mountains or in a big city with many high buildings.

    And third thing is, you will unlikely get that high precision with a mobile device. 10m is also already VERY accurate. You can be happy if you get an accuracy of 5 meters (and thats incredibly accurate). Everything lower than that is actually "military grade" of GPS and unlikely available for the endusers.

    Also the accuracy depends on the number of satellites. 3 satellites are minimum, which (at bad connectivity) may give you +/- 50 meters. With 5-7 satellites you can increases to ~10m.

    But as I said, it's unlikely you will get 7 satellites while inside your apartment/building (which is likely you are since you usually not programming outside :D)

    Also your while loop is not waiting very long, because the wait time is like a millisecond, because there is no yield methods for waiting, Read the docs for correct using inside unity
    Last edited: Apr 17, 2012
  3. MatD


    Apr 17, 2012
    Hello Tseng, thank you for your tips,

    I'm testing the whole thing outside in a car, so the GPS signal is pretty good even while driving (the speed corresponds to the one indicated by the car and to another GPS App).

    When driving inside of cities, with my Unity App I get differences of 20 to 50 km/h to the real speed due to gps position that are missed or completely faulty. Sometimes .. for 15-20 sec the GPS Position turns to be correct and the speed is correctly computed.

    I have another app built with Flex/AIR which also tracks and listen to the GPS data and I have no problem getting the correct position and compute the speed, so I don't think it's a device or inbuilt GPS accuracy problem. This fact leads me to think that the GPS Position in Unity is determined in another way.

    In your opinion should I "play" with the values of the (e.g.) Input.location.Start(1f,5f) in order to somehow get correct values ? In this case is the InvokeRepeating really needed if the values are only updated when the user moves away 10m from his previous position?

    Thank you!
    Last edited: Apr 17, 2012
  4. MatD


    Apr 17, 2012
    Ok I've finally solved the problem - or let's say found why it couldn't work - so it seems that the Input.location (in my case on Android 2.2) is indeed always computed from the Network and never from GPS explaining that I never could correctly compute a speed value. I don't know why it is the case...

    I ended up writing my own GPS Unity Plugin. If you are interested I've written a small tutorial to explain how to write this plugin, partly inspired by this post ( :
    zzzzz likes this.
  5. prashant1285


    Jul 18, 2014
    Thank you............
  6. KnightRiderGuy


    Nov 23, 2014
    Whoa!! Are you guys telling me you can input GPS data into a Unity 3D Ap? What about on the stand alone player?
    Say for instance if you were making a series of interactive screens for 5" touch screen LCD displays hooked up to a PC.
    Could that data be fed into your Unity scene to show a map and your location on that map with any kind of custom GUI layers you wanted to put over top of it?
    Sorry for the complex question but reading the posts above made me wonder if that was possible?
  7. AwesomeAlexx


    Jan 30, 2016
    I had the same problem for location, I noticed that depending of your connection it take more or less time to have a correct localization. So I just delay with a simple yield instruction before getting last datas and it worked better.
    But still with this Tseng is right you may not be 100% accurate.

    Hope it help.
  8. agentargyle


    Mar 8, 2016
    nelsonlarocca likes this.