Search Unity

How to get the accurate altitude from device with respect to the ground using unity?

Discussion in 'AR' started by zyonneo, Oct 18, 2018.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Hi I am working on an Indoor application for IOS application.I am trying to get latitudes,longitudes and altitudes.When I test the device the starting altitude value comes around

    3.67853-first floor(starting point)

    3.665541-second floor

    3.538336-Ground floor

    From the above values it is hard to know which floor the user is.Is there a way to get the correct altitude.

    Code (CSharp):
    1. void Start ()
    2. {
    3.     StartCoroutine(Getdata());
    4. }
    5.  
    6.  
    7. IEnumerator Getdata()
    8. {
    9.     // First, check if user has location service enabled
    10.     if (!Input.location.isEnabledByUser)
    11.         yield break;
    12.  
    13.     // Start service before querying location
    14.     Input.location.Start();
    15.  
    16.     // Wait until service initializes
    17.     int maxWait = 20;
    18.     while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
    19.     {
    20.         yield return new WaitForSeconds(1);
    21.         maxWait--;
    22.     }
    23.  
    24.     // Service didn't initialize in 20 seconds
    25.     if (maxWait < 1)
    26.     {
    27.         print("Timed out");
    28.         yield break;
    29.     }
    30.  
    31.     // Connection has failed
    32.     if (Input.location.status == LocationServiceStatus.Failed)
    33.     {
    34.         print("Unable to determine device location");
    35.         yield break;
    36.     }
    37.     else
    38.     {
    39.         // Access granted and location value could be retrieved
    40.         Locationinformation.text = "Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp;
    41.         print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
    42.     }
    43.  
    44.     // Stop service if there is no need to query location updates continuously
    45.     Input.location.Stop();
    46. }
     
  2. todds_unity

    todds_unity

    Joined:
    Aug 1, 2018
    Posts:
    324
    The data coming back from in the LocationInfo struct is data pulled directly from the device. It does contain horizontal and vertical accuracy values that inform how accurate the report location has been measured. But there is no method to increase this accuracy from your code.

    Todd
     
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I can increase the accuracy?.If so how?
     
  4. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    @todds_unity Hey i read in another blogs that unity only allows only few decimal points of the latitudes and longitudes.Any idea how to take all points from a location. eg;latitudes=7.637253725372637263232332,longitudes122.21212121212121212121212.So that we could take sthe accurate position from GPS of a device.
     
  5. todds_unity

    todds_unity

    Joined:
    Aug 1, 2018
    Posts:
    324
    @zyonneo ,

    When starting the Unity LocationService, you may specify an accuracy level that maps to a device specific setting.

    The LocationService documentation describes how this information is used.
    https://docs.unity3d.com/ScriptReference/LocationService.Start.html

    iOS has mappings to 3-different levels of accuracy as documented here:
    https://developer.apple.com/documentation/corelocation/getting_the_user_s_location?language=objc

    The most accurate service being "Standard location service" which will provide the most accurate location data but will also consume the most power.


    Additionally, the longer the app is running with the location service active, the more accuracy the device will be able to achieve.


    In terms of your second question, Apple's location service does provide location data in 8-byte double values.
    https://developer.apple.com/documentation/corelocation/cllocationdegrees?language=objc

    whereas the Unity LocationInfo provides location data as 4-byte float values.
    https://docs.unity3d.com/ScriptReference/LocationInfo-latitude.html

    Thus, there will be some loss of precision in that conversion.


    If you do wish to get the original 8-byte data from the device, you would need to disable the Unity LocationService and interact directly with the iOS Core Location service yourself.

    https://developer.apple.com/documentation/corelocation/getting_the_user_s_location?language=objc

    Todd
     
  6. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    @todd_unity Inorder to interact with iOS Core Location, do we need to use plugin?
     
  7. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
  8. todds_unity

    todds_unity

    Joined:
    Aug 1, 2018
    Posts:
    324
    @zyonneo ,

    You will have to write the Obj-C or Swift code yourself to interact directly with iOS Core Location. Or the asset store might also have a package that might help.

    Todd