Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    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.         {
    27.             while(Input.location.status == LocationServiceStatus.Initializing  wait>0)
    28.             {
    29.                 wait--;
    30.             }
    33.             if (Input.location.status == LocationServiceStatus.Failed) {
    35.             }
    36.             else {
    37.                 gpsInit = true;
    38.                 // We start the timer to check each tick (every 3 sec) the current gps position
    39.                 InvokeRepeating("RetrieveGPSData", 0, 3);
    40.             }
    41.         }
    42.         else
    43.         {
    44.             GameObject.Find("gps_debug_text").guiText.text = "GPS not available";
    45.         }
    46.         #endif 
    47.     }
    49.     void RetrieveGPSData()
    50.     {
    51.         currentGPSPosition = Input.location.lastData;
    52.         string gpsString = "::" + currentGPSPosition.latitude + "//" + currentGPSPosition.longitude;
    53.         GameObject.Find("gps_debug_text").guiText.text = gpsString;
    54.     }
    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. zentaiguy


    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