A Flash Developer Resource Site

# Thread: Turn latitude/longitude into X,Y coordinates?

1. ## Turn latitude/longitude into X,Y coordinates?

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?

Thanks for any help!

2. i found this post. This is sort of what i'm after, but this doesn't fit exactly what i'm trying to do, i don't think.

Anyone have any ideas??

3. you may want to try this code by exorcyze (modified a bit)
Code:
```var x = 0;
var y = 0;

function getPoint(lat, lon, mapwidth, mapheight) {
x = (180+lon) * (mapwidth / 360);
y = (90-lat) * (mapheight / 180);
}```
Usage (assuming the Map movie clip is "centered" at the top left corner of the map):

Code:
```getPoint(32.777654, -79.933819);
placeDot(Map._x + x, Map._y + y);```

4. EDIT: ignore this post :P

http://mathworld.wolfram.com/SphericalCoordinates.html

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 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.

- Martin

This forum needs MathML :P

5. ## woah.

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.

Go easy on me...math scares me

6. 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.

7. Look at http://www.mainmaps.com/world_lat_long/ ( enter lat/lon in "orange" panel and press "SET" ).
Send me PM if you're interested.

8. 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?

THanks everybody!!!

9. 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.

10. 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.

11. 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.

12. ## plotting LAT/LONG to X, Y, Z

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.

Anyone have any idea what is wrong here?

Here is the ActionScript that I am using:

xPos = (app.radius) * Math.cos(longitude) * Math.cos(latitude);
yPos = (app.radius) * Math.sin(longitude) * Math.cos(latitude);
zPos = (app.radius) * Math.sin(latitude);

13. Originally Posted by miquael
xPos = (app.radius) * Math.cos(longitude) * Math.cos(latitude);
yPos = (app.radius) * Math.sin(longitude) * Math.cos(latitude);
zPos = (app.radius) * Math.sin(latitude);
the Globe isn't correct ball (sphere).
Look at http://www.posc.org/Epicentre.2_2/Da...e/eu_cs34.html

14. think so?

15. well, i solved it with this:

// convert lat/long to radians
latitude = Math.PI * latitude / 180;
longitude = Math.PI * longitude / 180;

latitude -= 1.570795765134; // subtract 90 degrees (in radians)

// and switch z and y
xPos = (app.radius) * Math.sin(latitude) * Math.cos(longitude);
zPos = (app.radius) * Math.sin(latitude) * Math.sin(longitude);
yPos = (app.radius) * Math.cos(latitude);

now all plots about right (a little more adjustment needed on the long)

16. ## Turn latitude/longitude into X,Y coordinates?

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.

17. ## Turn latitude/longitude into X,Y coordinates?

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

18. Hi miquael,

We are also facing a similar kind of issue with this. We want to try your solution. May i know what app.radius really means.

-skanumuri

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap

Click Here to Expand Forum to Full Width