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:
Here is a representation of what the UI looks like on the user's mobile device (lines are not correctly plotted):
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;
}