4

So here is the basic problem. I am currently working on a GPS system in C# in Unity 3D (the person that has given us the assignment is making us use this program, so I can't do it in anything else).

Now I've run in to a small problem, basically we are able to request (what we think are decimal) coordinates from an android phone, but now we are trying to convert those coordinates to X, Y, Z coordinates. Preferebly X and Z, because we do not actually need height. However everything we have been finding on the internet so far has been for a conversion to a sphere map where as we just have a basic flat digital map.

If anyone knows how to convert the coordinates we have to the basic X and Z coordinates (so our longitude and latitude) it'd be amazing.

To quickly note I am not sure if the sort of coordinates we have are actual decimal coordinates so this is what they look like:

Latitude: 53.228888 Longitude: 6.5403559

these coordinates should end up on "Wegalaan 3, Groningen, The Netherlands" if you would look them up on a map.

Thanks already!

EDIT: (this is also in the comments)

Sorry if it might be confusing. Honestly I only half understand how all this works, anyways to clear some things up. I am currently working in Unity with a simple 2D map I got from the internet of the city I live in (Groningen, The Netherlands) and I am trying to basically take GPS coordinates I get from my android phone and then show them on that map with a red dot, however to do this I need to be able to move the red dot to the right coordinates on the map. What I am trying to do is convert the GPS coords (lon and lat) to X and Z (Unity3D flat coordinates, may also just be X and Y) so that if I align the map right I get a small GPS system for just my city. If you are curious as to why I am doing this it is simply because a friend of mine and me are trying to build a game using our city and this GPS system as a basis

EDIT2:

except that I'll be honest I have no idea how cartesian coordinates work, but they seem to be what I am looking for yes :P Coordinates on a flat plane and with X,Y coords I mean basically just coordinates I could use in Unity3D on a flat 2D plane which is what I am working in.

EDIT3:

Thanks for the answers, to start. This is not a duplicate, secondly my friend and I already found the stackoverflow topic you sent me, but it seems to not be working for us (maybe we did something wrong). Basically the north and south distance between different places we tested with that formula have worked, however the east west distance between them was way bigger than it should have been. We think it might be because that formula was meant for a spherical earth, but maybe we did something wrong. If someone could explain further that'd be amazing!

EDIT4:

We are sure it can't be our map that is wrong in any way, because we have aligned it with multiple locations. We got the coordinates for these locations and then used this website: http://www.gpscoordinaten.nl/converteer-rd-coordinaten.php to convert it to XY coordinates and then used these XY coordinates to check if our map would align properly. It did, so we are sure there is some problem with the maths we are using and not with our actual map.

EDIT5: Removed many, many grammatical errors. It's way too hot over here to be writing properly right now, so I am very very sorry if any of this makes no sense. just let me know and I'll edit to try and explain what we are trying to do.

EDIT6: Found my own asnwer, it is down in between all the other answers if you wanna see what I did to fix my problem

LAKster
  • 151
  • 1
  • 1
  • 12
  • I'm not quite sure what you mean by this, but latitude is the horizontal lines and longitude is the vertical ones. Because of the curve of the Earth, the distance between two lines of longitude goes down as the latitude becomes higher, so if you wanted to think of it as a rectangular grid it doesn't really work. Usually in geographic software you'll convert from a geographic coordinate system to a projected coordinate system for display. – maniak1982 Aug 31 '15 at 12:49
  • Haven´t you already postedt that question earlier today? However: What do you mean with X,Y-coords? Should it be cartesian coordinates? If so which unit do you need? There are many many params which affect such a transformation. – MakePeaceGreatAgain Aug 31 '15 at 12:53
  • 1
    possible duplicate of [Converting from longitude\latitude to Cartesian coordinates](http://stackoverflow.com/questions/1185408/converting-from-longitude-latitude-to-cartesian-coordinates) – vidstige Aug 31 '15 at 12:54
  • See here http://stackoverflow.com/a/1185413/363437 – vidstige Aug 31 '15 at 12:54
  • @maniak1982 Sorry if it might be confusing. Honestly I only half understand how all this works, anyways to clear some things up. I am currently working in Unity with a simple 2D map I got from the internet of the city I live in (Groningen, The Netherlands) and I am trying basically take GPS coordinates I get from my android phone and show them on that map with a red dot, however to do this I need to be able to move the red dot to the right coordinates on the map. What I am trying to do is convert the GPS coords (lon and lat) to X and Z so... (continues in my next comment) – LAKster Aug 31 '15 at 13:02
  • that if I align the map right I got a small GPS system for just my city. If you are curious as to why I am doing this it is simply because a friend of mine and me are trying to build a game using our city and this GPS system as a basis – LAKster Aug 31 '15 at 13:04
  • Please add this information as edit to your question instead of commenting. Thus it´s way easier to find. – MakePeaceGreatAgain Aug 31 '15 at 13:06
  • @HimBromBeere Yeah I did post it earlier as well, but some mean people told me I was an idiot for even asking it and downvoted it. I reported the people and deleted that post to get rid of the hate, then reposted the question in the hope of a serious answer. Please see my answer to maniak to kinda understand what I mean... except that I'll be honest I have no idea how cartesian coordinates work, but they seem to be what I am looking for yes :P Coordinates on a flat plane and with X,Z coords I mean basically just coordinates I could use in Unity3D which is what I am working in. – LAKster Aug 31 '15 at 13:07
  • @vidstige Thanks for the answer, to start. This is not a duplicate, secondly my friend and I already found the stackoverflow topic you sent me, but it seems to not be working for us (maybe we did something wrong). Basically the north and south distance between different places we tested with that formula have worked, however the east west distance between them was way bigger than it should have been. We think it might be because that formula was meant for a spherical earth, but maybe we did something wrong. If someone could explain further that'd be amazing! – LAKster Aug 31 '15 at 13:10
  • You should try to find the projection of the map. What you're going to need to do is project the latitude and longitude (geographic coordinate system) onto the map, and you won't be able to do that implicitly. You could try getting the coordinates of the corners of the map, then calculating the coordinates of the GPS position in relation to those points. It won't be accurate but it will be quick and dirty. – maniak1982 Aug 31 '15 at 14:06

2 Answers2

2

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

LAKster
  • 151
  • 1
  • 1
  • 12
  • Thanks for your reply, LAKster. :) My (0,0,0) is right at the center of the earth sphere in my Unity project. The north pole is at (0, RADIUS_SPHERE, 0). Should I keep those 53. and 6. or what? – N3sh Feb 21 '16 at 13:06
  • Hey, I'm sorry I am replying now instead of when you posted the question. Honestly I am not sure. By what it looks like you are using an actual sphere instead of a flat map like I am. If you are just using a flat map this is what those numbers mean. The 53. and 6. are the gps coordinates of the real world place on my map that I have at the 0 X and 0 Z point in Unity. Hope this helps :D – LAKster Apr 12 '16 at 12:46
  • @LAKster I'm using a flat map, how should I know what are the number to use instead of 53.something and 6.something. Since I tried the code you've written in your answer but it gave me a big x and z – Mohamad Mousheimish Jul 17 '19 at 07:31
0

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

reden
  • 968
  • 7
  • 14
  • On Mercator-projections latitude +/-90 is not printable as they are inifintely far away – MakePeaceGreatAgain Aug 31 '15 at 13:05
  • I'll be honest here. I started working on this last week and only learned half of what I know today. I did stumle on mercator projections, but I have no idea how they work or how exactly they should influence what I am doing. I more or less get that they are just literal flat maps of places, and as far as I know the map I am using is exactly that. We did test the map as well to make sure it would work with coords. I am just having trouble determening the maths to do the conversion to X and Y coords myself – LAKster Aug 31 '15 at 13:23
  • @HimBrommBeere You are correct. I'll update the answer accordingly. – reden Aug 31 '15 at 13:38
  • Thanks for the link. I'll look at it tomorrow, because currently I am home and just do not feel like working XD Thanks for the answer and I think I might have an idea already, but I'll have to look in to the link you put here to see if I can actually use that idea. – LAKster Aug 31 '15 at 18:48