0

How can I convert longitude and latitude to Unity 3D coordinates based on a longitude and latitude plotted to 0, 0 in Unity?

There are a number of suggestions made about how to do this, but I have yet to see a complete and concise answer to this question.

I need to plot a series of polylines in Unity 3D that represent lines drawn on a map. Each point in the polyline is represented by a latitude and longitude. The coordinates are represented as EPSG:4269 NAD83.

The scenario begins when the user's longitude and latitude is sent to a MongoDB server as a spatial query, and the server responds with a geojson feed containing an array of coordinates that represent each point in the polyline.

The user in Unity3D is positioned directly in the center of the map (0, 0). The compass within the user's device points the Z-Axis to the North. The X-Axis represents east/west. The Y-Axis represents altitude.

The goal is to draw lines on the screen that represent geospatial objects in reference to the user. The program is able to correctly make requests, parse the geojson feed, and plot lines BUT the lines are obviously not correctly positioned without the correct conversion from latitude/longitude to Unity3D cartesian.

This formula is the basically the last piece of the puzzle. I am writing the program for Unity3D using C#.

Here is a representation of how the lines look in an online map. The blue pin represents the user's location:

enter image description here

Here is a representation of what the UI looks like on the user's mobile device (lines are not correctly plotted):

enter image description here

Code sample I've managed to munge up while trying to make it all work:

//
// Longitude and Latitude to Unity
//
public Vector3 LatLonUnity(double longitude, float elevation, double latitude){
    // code based on http://stackoverflow.com/questions/32311647/converting-gps-coordinates-to-x-y-z-coordinates

    float u_lat = Convert.ToSingle ((latitude - DrawLines.user_latitude) / 0.00001 * 0.12179047095976932582726898256213);

    float u_lon = Convert.ToSingle ((longitude - DrawLines.user_longitude) / 0.000001 * 0.00728553580298947812081345114627);

    Vector3 geo_unity = new Vector3 (-u_lon, elevation, -u_lat);

    return geo_unity;

}
Twitch
  • 664
  • 1
  • 7
  • 26
  • Uh oh, that sounds like a hint the question wasn't clear. Did I overthink the explanation? – Twitch Jun 09 '16 at 01:17
  • Sorry no, it was just a motivational joke. :) I think detail-wise it is very good but I think you need to say more about what problem you need help solving. Something about the lines aren't drawing right? Any code to show? Wishing you well –  Jun 09 '16 at 02:11
  • @mickyd thanks :o) added a code sample - hopefully it still makes sense; I've tweaked it over and over trying to figure out why the Unity results don't look anything like the map results. – Twitch Jun 09 '16 at 05:07
  • Thanks for adding the code sample. I don't know much about geo-spacial stuff but hopefully someone else will come along and can help you with your code. Good luck fellow Unity user :) –  Jun 09 '16 at 05:14

1 Answers1

0

Latitude θ and Longitude φ are two of the components in spherical-polar coordinates (the other being the Radius of the planet r, which you must also specify).

enter image description here

For coordinates at elevation h, simply replace enter image description here