Search Unity

  1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    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.         {
    27.             while(Input.location.status == LocationServiceStatus.Initializing  wait>0)
    28.             {
    29.                 wait--;
    30.             }
    31.            
    32.  
    33.             if (Input.location.status == LocationServiceStatus.Failed) {
    34.  
    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.     }
    48.    
    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.     }
    55.  
    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
  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/
     
    zzzzz likes this.
  5. prashant1285

    prashant1285

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

    zentaiguy

    Joined:
    Nov 23, 2014
    Posts:
    392
    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:
    1
    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:
    3