How to convert The Latitude and Longitude values into XYZ coordinates of Unity?

  • Unity version: 2020.3.17f1
  • ARDK version: 1.2.0
  • Device type & OS version: Android - OnePlus 9 Pro
  • Host machine & OS version: Windows 10

Description of the issue:

Hi All, I am developing a Location Based AR demo application using the LightShip SDK. I have to convert the Latitude and Longitude values into the XYZ coordinates of Unity. I am attaching the code reference that I have tried, Please check:

    var rad = Math.PI / 180;
    var lat = TargetLatitude * rad - currentLatitude * rad;
    var lon = TargetLongitude * rad - currentLongitude * rad;
    var a = 6378.137* 1000;
    var e2 = 0.00669437999014;
    var N = a / Math.Sqrt(1 - e2 * Math.Pow(Math.Sin(lat), 2));

    var x = N * Math.Cos(lat) * Math.Cos(lon);
    var y = N * Math.Cos(lat) * Math.Sin(lon);
    var z = (1 - e2) * N * Math.Sin(lat);
    Vector3 XYZ = new Vector3((float)x,(float)y,(float)z);

I am getting the XYZ coordinates like this (6378137.0, -11.0, 5.8) based on the code reference that I have shared.

Can anyone please help me how I can get the XYZ coordinates of Unity based on the Latitude and Longitude?

This is what I’m using for now…

https://docs.mapbox.com/mapbox-unity-sdk/api/unity/Mapbox.Unity.Utilities.Conversions.html

Hi @Dudley_HK, Thank you so much for your reply. I am developing the location based AR demo using the LightShip SDK.

Can anyone please help me out to get the Unity XYZ coordinates from Latitude and Longitude?

Thanks in advance.

Hi Akhil, you’ll need to implement functions to go to/from GPS coordinates (lat, lon) and the cartesian (x, y, z) coordinate system of the tracking system. This can be done by using GPS/compass while the AR Session is running, but the implementation needs to be done on your side. Thanks.

Hi @Moranda_Nalls1, Thank you so much for your response. Can you please provide some references for this implementation?

Hi Akhil, take a look at Scripting API: Compass reference, and Scripting API: LocationService.Start documentation to assist with your project. I hope this helps. Thanks.

Hi @Moranda_Nalls1, Thank you so much for your references. I was able to get the x, y, z values based on the LocationService.Start documentation this reference. I want to make the Gameobject direction to be the same as the specific GeoLocation Direction. I have tried the Compass reference but it is not giving the result that I am expecting.

Cab you please help me to make the Gameobject direction to be same as the specific GeroLocation direction?

Thank you.

Hello Akhil, you mentioned Compass didn’t work. Did you happen to try Compass.trueHeading? If not, this will solve your issue. If yes, double check your code and please provide what happens when it’s implemented.

This is the math I use

public static class ExtraMath
        {
            public struct LocationToMeters
            {
                public static double Lon(double lon)
                {        
                    var x = lon * 2 * Math.PI * 6378137 / 2 / 180;
                    return x;
                }

                public static double Lat(double lat)
                {        
                    var y = Math.Log(Math.Tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
                    y = y * 2 * Math.PI * 6378137 / 2 / 180;
                    return y;
                }
            }
        }

As far as I know Altitude is already in Meters.
Latitude and Longtitude needs to be “destretched” from a spherical mapping to get a 2D coordinate system that alligns with unitys.
You can plug in the lat and long from whatever source you want, I used this in combination with Mapbox because the whole compass service setup is done already.
And 1 Meter is exactly 1 Unity Unit if youre wondering

1 Like

hello akhil how did you get the x,y,z values since i am also in the same problem

Hi Francis, Akhil used these tools to convert to XYZ coordinates.

Scripting API: Compass reference , and Scripting API: LocationService.Start documentation

Since this thread has been closed, would you like to open a new one to get some more visibility on this question?

yes i fixed it thanks compass fixed the problem