Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GPS Input.Location accuracy question

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

  1. MatD

    MatD

    Joined:
    Apr 17, 2012
    Posts:
    3
    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):
    1.  
    2. #define ANDROID
    3. using UnityEngine;
    4. using System.Collections;
    5. using GeoUtility;
    6. using GeoUtility.GeoSystem;
    7. using System.Timers;
    8.  
    9. public class GPSManager : MonoBehaviour {
    10.    
    11.     bool gpsInit = false;
    12.     LocationInfo currentGPSPosition;
    13.  
    14.     void Start () {
    15.  
    16. #if PC
    17.         Debug.Log("On PC / Don't have GPS");
    18. #elif !PC
    19.        
    20.         //Starting the Location service before querying location
    21.         Input.location.Start(0.5f); // Accuracy of 0.5 m
    22.        
    23.         int wait = 1000; // Per default
    24.        
    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.             }
    32.            
    33.  
    34.             if (Input.location.status == LocationServiceStatus.Failed) {
    35.  
    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.     }
    49.    
    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.     }
    56.  
    Thank you for your help and tech advices!
    MatD
     
  2. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    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)

    edit:
    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
    KennaGetsTrifected likes this.
  3. MatD

    MatD

    Joined:
    Apr 17, 2012
    Posts:
    3
    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

    MatD

    Joined:
    Apr 17, 2012
    Posts:
    3
    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 (http://forum.unity3d.com/threads/100751-Android-Plugin-JNI-Question) :

    http://www.mat-d.com/site/unity-gps...ndroid-plugin-for-unity-with-eclipse-and-ant/
     
    Siggytron and zzzzz like this.
  5. prashant1285

    prashant1285

    Joined:
    Jul 18, 2014
    Posts:
    1
    Thank you............
     
  6. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    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

    AwesomeAlexx

    Joined:
    Jan 30, 2016
    Posts:
    18
    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

    agentargyle

    Joined:
    Mar 8, 2016
    Posts:
    5
    nelsonlarocca likes this.