I'm working on a map that will display a person's location with a dot. We have the geographical coordinates in Latitude/Longitude form. So, i'm totally lost (i'm TERRIBLE at math) as to how i can turn latitude/longitude info into X and Y coordinates so that flash can move the little dot around to the right location.
For instance: The following variables are passed into flash: Lat: 32.777654 Long: -79.933819. NOW, we need to translate that into an XY position inside the mc with the map graphic in it. I'm guessing this can be down with a mathematical conversion/equation but i have no clue how to do it. Any ideas?
This is a two step problem. First you must convert your spherical coordinates into cartesian coordinates, and then you must project them on to the xy plane.
1) Spherical coordinates are of the form .
is the radius of your world.
is the azimuth coordinate or in this case the longitude.
is the zenith coordinate or in this case the colatitude. This means its like the latitude except instead of being measured from the equator, it's measured from the northpole. The colatitude is just 90-latitude.
So now you've got the spherical coordinate (radius_of_world, longitude, 90 - latitude). You can change that to (x,y,z) using formulae 4, 5, and 6 in the link above.
x = radius_of_world * cos(longitude) * sin(90 - latitude)
y = radius_of_world * sin(longitude) * sin(90 - latitude)
z = radius_of_world * cos(90 - latitude)
Applying trig identities and actionscript we get:
x = radius_of_world * Math.cos(longitude) * Math.cos(latitude)
y = radius_of_world * Math.sin(longitude) * Math.cos(latitude)
z = radius_of_world * Math.sin(latitude)
Now that you have a point (x, y, z) you can project this on to the xy plane. This is a little tricker of a concept to grasp so I'll just show the code instead:
projected_x = x * focal_length / (focal_length + z);
projected_y = y * focal_length / (focal_length + z);
where focal_length is the distance from the xy plane to the viewers eye. It is a constant and a good value is generally around 150-200.
Thanks for the response Somar. Two things: i'm thinking that last post (the one i'm supposed to ignore) is probably overkill. My map is of a city's downtown...very small area...so i don't think i need to take into account the earth's curvature or do i not understand what i'm talking about?
Second: on the previos post (with the code from exorcyze)....are the mapwidth and mapheight variables the actual pixel size of my map graphic? If so, i'm a little confused as to how this works.
Well assuming your map is a Mercator projection or of the sort you should be fine with exorcyze's algorithm because meridian lines are distributed at equal intervals eg. every 5 cm on the map is 10 degrees.
For a map of a city however it doesnt really matter if its a Mercator projection or not because the curvature of the earth is not apparent on such a small scale.
Anyways, I don't have much time to explain it so I just threw together an fla.
Somar: you are my hero!! Thanks for breaking it down...i'm gonna disect your fla and see if i can get it working on mine. One quick question though: do i just do a trial & error to find the min/max latitude & longitude of the map i have...or is there a faster way?
For my example I made them up and over exagerated them. For a city... I'd imagine you're going to be getting into the decimals... the left edge of the city might be at say 60.2 degrees longitude and the right edge at 60.7 degrees... I don't really know... :P You're best bet is to find an actual map of the city you're working with and see what the dimensions are.
you're right. i looked up the coordinates and the min/max latitude and longitude for the city map i'm using are like a bazillionth of a point apart (ie. the difference between my max & min longitude values is only .061276 of a point).
From playing with your flash file and mine, the more decimal places there are on the lat/lon values, the less accurate the star becomes. For instance, on your map, the lat/lon numbers were easy and big differences between gridlines...and i can get that star to go exactly where i want it. BUT, when i reduced all your lat/lon values down (relative to the same format on my map) the star's placement becomes very inaccurate.
So i'm guessing that since we're dealing with such a small area, that my min/max lat/lon values will have to be super accurate, right? I was hoping i could just fudge it.
look at http://www.mainmaps.com/poligon_test/
Open " Map list" adown. For example, Press "chicagoname" -map. You can test lat/lon using street-name by http://www.stevemorse.org/jcal/latlon.php .
This project has a mistake of the calculations less 0,01 ( lat/lon ).
But do the mistake of the calculations less 0,0001 ( lat/lon ) much it is difficult.
Remark. The exist algorithm without "min/max lat/lon". This not necessary.
You must have exactly lat/lon of any two points from map.
So I am working on an open-source 3D Flash mapping application. You can see and download all the source code here (including the LAT/LONG data): http://code.google.com/p/meadanglobe
I found the calculations for converting LATITUDE and LONGITUDE to X, Y, Z coordinates above, yet I am just not getting accurate results! There are actually 2 issues:
1) the nodes do not appear to be in correct locations relative to each other (ie., Rome and Johannesburg are right next to each other, and San Francisco and New York City are about half way around the world from eachother!)
2) also, the nodes are not not in the right places (ie, New York City is at the South Pole!)
I assume that the second issue will be easier to solve (relative x, y, z adjustment) once the first issue is resolved -- which appears much more variant.
I'm working on a map that will display a person's location with a dot. We have the geographical coordinates in Latitude/Longitude form. So, i'm totally lost (i'm TERRIBLE at math) as to how i can turn latitude/longitude info into X and Y coordinates so that flash can move the little dot around to the right location.
For instance: The following variables are passed into flash: Lat: 32.777654 Long: -79.933819. NOW, we need to translate that into an XY position inside the mc with the map graphic in it. I'm guessing this can be down with a mathematical conversion/equation but i have no clue how to do it. Any ideas?
you're right. i looked up the coordinates and the min/max latitude and longitude for the city map i'm using are like a bazillionth of a point apart (ie. the difference between my max & min longitude values is only .061276 of a point).
From playing with your flash file and mine, the more decimal places there are on the lat/lon values, the less accurate the star becomes. For instance, on your map, the lat/lon numbers were easy and big differences between gridlines...and i can get that star to go exactly where i want it. BUT, when i reduced all your lat/lon values down (relative to the same format on my map) the star's placement becomes very inaccurate.
So i'm guessing that since we're dealing with such a small area, that my min/max lat/lon values will have to be super accurate, right? I was hoping i could just fudge it.
I'm working on a map that will display a person's location with a dot. We have the geographical coordinates in Latitude/Longitude form. So, i'm totally lost (i'm TERRIBLE at math) as to how i can turn latitude/longitude info into X and Y coordinates so that flash can move the little dot around to the right location.
For instance: The following variables are passed into flash: Lat: 32.777654 Long: -79.933819. NOW, we need to translate that into an XY position inside the mc with the map graphic in it. I'm guessing this can be down with a mathematical conversion/equation but i have no clue how to do it. Any ideas?
you're right. i looked up the coordinates and the min/max latitude and longitude for the city map i'm using are like a bazillionth of a point apart (ie. the difference between my max & min longitude values is only .061276 of a point).
From playing with your flash file and mine, the more decimal places there are on the lat/lon values, the less accurate the star becomes. For instance, on your map, the lat/lon numbers were easy and big differences between gridlines...and i can get that star to go exactly where i want it. BUT, when i reduced all your lat/lon values down (relative to the same format on my map) the star's placement becomes very inaccurate.
So i'm guessing that since we're dealing with such a small area, that my min/max lat/lon values will have to be super accurate, right? I was hoping i could just fudge it. shae marks