Converting GPS coordinates to X, Y, Z coordinates

岁酱吖の 提交于 2019-12-03 17:22:34

By now I have found the answer to my own question (actually found it a little while ago already, but totally forgot to post it here)

Basically I made a little formula of my own that multiplied the coordinates with a set number (depending on wether or not it is the x or y axis) that is the difference between two set coordinate points. These two points are the outer points of my map.

Basically by doing this I can get quite accurate measurements I haven't even had a meter difference to my actual position yet.

I know this sounds a bit vague and I don't entirely know how to explain it, but for anyone interested here is the code I use:

void RetrieveGPSData()
{
    currentGPSPosition = Input.location.lastData;
    System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
    dateTime = dateTime.AddSeconds(currentGPSPosition.timestamp);

    float z = latToZ (Input.location.lastData.latitude);
    float x = lonToX (Input.location.lastData.longitude);

    this.transform.position = new Vector3 (x, 0f, z);

    gpsString = ("Last Location: " + Input.location.lastData.latitude.ToString () + " " + Input.location.lastData.longitude.ToString () + " " + Input.location.lastData.altitude.ToString () + " " + Input.location.lastData.horizontalAccuracy.ToString () + " " + dateTime.ToShortDateString() +" "+ dateTime.ToShortTimeString());
}

float latToZ (double lat){

    lat = (lat - 53.178469) / 0.00001 * 0.12179047095976932582726898256213;
    double z = lat;

    return (float)z;
}

float lonToX (double lon){

    lon = (lon - 6.503091) / 0.000001 * 0.00728553580298947812081345114627;
    double x = lon;

    return (float)x;
}

Now for anyone wondering why I took the 53.something and the 6.something off the lon and lat it is because the coordinate of my 0 X and 0 Z point in Unity which corresponds to the bottom left corner of my map, which is one of the two points I talked about that I use for this calculation.

I hope this helps anyone else who might ever be stuck on something similar and if you have any questions feel free to ask them.

-LAKster

Does the map have mercator projection? Is it a world map? If so, the top of the map is usually around latitude 85, latitude 0 at the center vertically. longitude 0 is at greenwich (usually center horizontally), 180/-180 is at the antimeridian/date line, at the left and right edges of the map.

When the latitude is -180, x would be 0, when it's 180, x == width of map.

This link should tell you what you need

Have you seen this script? It does the opposite of what you're looking for, but you can reverse the formulas.

http://wiki.unity3d.com/index.php/GPS_Global_Positioning_System

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!