Avatar of jloring
jloring asked on

Convert Latitude/Longitude values to x,y on a map

Hi, desperate for some help.
I need to place red dots on a map of the United States. The dots represent the locations of several companies, universities and cities on a map of the U.S. We're using Macromedia Flash to display the map and plot the dots. Obviously we can do this manually using an external file, but long story short this data is kept in a database and will change frequently and without warning.

I only have the the latitude and longitude of these universities/companies/cities. I need to convert the latitude/longitude to x,y coordinates on the map. Once I have the x,y plotting them on the map is simple.

I have a map I obtained from http://nationalatlas.gov/natlas/ so I trust it's accuracy. I know the longitude/latitude of the 4 corners of this map. I have been playing with various formulas to no avail. I'm close, but I'm not close enough.


Much thanks.
Math / Science

Avatar of undefined
Last Comment

8/22/2022 - Mon

http://nationalatlas.gov/natlas/ is a privelaged site (I can not view it) the relationship depends on the projection used for the map.

Try this link: http://nationalatlas.gov/natlas/NatlasStart.asp
This should work great. It's an amazing resource for generating maps.

I am being told that I'm using a conic projection, but I need to use a spherical projection so that the lines of longitude and latitude are parallel and perpindicular. This makes a lot of sense, but where could I find such a map online?

If I have such a map, what formulas could I use?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

The equations for Mercator projection are


is the Map the continental USA?. The problem with no-global maps is that some times local appoximations to the earth in terms of an ellipsoid is used so accurate conversion of long/lat to XY  can be messy. I would find the map you like and then concentrate on working out the conversion.

Yes, the map is the continental United States.

Here is the working model I have developed using a conic projection map:

I think I would better off using a Spherical projection map of the continental U.S. But I don't know where I can find one that also has the lat/long displayed on it.

OK, since you are "desparate" here is a way that will work for you to convert a given lattitude and longitude into the corresponding x, y coordinate values on your map.  The idea is to set up a table that links the lattitude and longitude coords to the x-y coords.

Print your map and sketch in additional lattitude and longitude lines to increase the detail.  You can trace them "parallel" to the official ones.  Then make a list associating each longitude-lattitude pair with the corresponding x-y pair.  

Write your program to do a look-up on this table.  For more precision, do interpolation to get a better x-y for lattitude-longitude that falls between items in the list.

You can increase the precision you get by managing the detail in your table of lattitude-longitude to x-y and by using interpolation to handle in-between values.

Obviously this isn't perfect.  But perfection is not usually the best way to solve an urgent real-world problem.  Please, let me know if you find this useful.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

I have'nt thoroughly tested it , it is a simple blinear interpolation.

Now got the x coord spot on with

x=1238.3124 - 9.7689*lon

working on the y

This is as good as I can get it  for the map http://www.graphiteproductions.com/mapping/flash/map.php
( i am not sure how accurate your lat data is)

y=632.98168 - 9.07797 * lat - 0.06291* lat*lat
x=1238.3124 - 9.7689*lon
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

You might want to try y=717.784412711722 - 13.74154357099229 * lat, I am not sure but I think San Francisco Latitude data is very slightly off

Gwyn, this is very close to a formula I've been working with myself (as a non-mathematician). I will try your variation right now. I have switched from a conic map projection to a spherical (like a Mercator map). There still seems to be some distortion to the map, but it's definitely better than working with a conic map.
Thanks. Jennifer.

If you exclude the SF y/lat data this pair is within a fraction of a pixel

x=1238.3124 - 9.7689*lon
y = 715-13.7*lat
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

If you post a larger well distributed (x,y) (lon,lat) data set I can data fit a curve to them for you.
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Hey I've had the same problem here I posted this question:

This seems to be what I need to get the x and y just where would I place any of these formulas to get the results? In a calculated field in DB or in an asp page or in flash? I'm just lost but it looks like the answer, mine can be a few pixles off I just need a display

The map I have (or will have) is going to be a Mercator (Spherical) projection. This way I don't have to deal with the curvature of the Earth crap and introduce trigonometry into my formulas.

Everything suggested by everyone gets me very, very close. For some placements I am dead on, but for others I'm almost 10 pixels off (x and/or y). It's very, very frustrating. Of course, it might just be the quality of the map I'm using too. I wish I could find a 100% qualified map to work with.
Your help has saved me hundreds of hours of internet surfing.

I think it is just the offset if u use my comments above i have quoted some offset, jus add it and your work is done. I think that 10 pixels or anything will remain constant throughout the map. For x & y axes both, the value can be same or different.



Hey everyone,
I've finally got this working using a very simple set of formulas (in PHP). The biggest battle is getting an accurate Mercator map and knowing your map's endpoints. Here's the map solution we used (you can save the output as an Adobe Illustrator file):

I created a map using the above tool with the following endpoints: N: 53, E: -66.54, S: 23, W: -125.49
This creates a nice Continental US & partial Canadian map. Remember to check the "download" box if you want to save the output as a Illustrator file!

I imported the map into Macromedia Flash MX. The map is 600 x 396 (pixels). Here's the working model on my Web site:

Flash reads a flat text file containing the following result sets (as example):
&nam1=SF State
&loc1=San Francisco, CA

&nam2=Wayne State
&loc2=Detroit, MI

&nam3=University of Texas
&loc3=Austin, TX

You get the idea. You could use PHP to obtain this from a database dynamically rather than use a flat text file, if you choose.
Generating the x,y values from long/lat is, of course, the real trick.

Here's how I derived the y coord:
function findYcoord($myLat) {
      $mapHeight = 396;
      $rfactor = 290;
      $radBtm = deg2rad(23);
      $radPixel = deg2rad($myLat);
      $sinRadBtm = sin($radBtm);
      $sinRadPixel = sin($radPixel);
      $convHtBtm = $rfactor * log((1 + $sinRadPixel)/(1 - $sinRadPixel));
      $convHtPixel = $rfactor * log((1 + $sinRadBtm)/(1 - $sinRadBtm));
      $myTotHt = abs($convHtPixel - $convHtBtm);
      $myYcoord = round($mapHeight - $myTotHt, 3);
      return $myYcoord;

You simply pass in the latitude value, which is later converted to a radian.
$mapHeight is the height, in pixels, of my map in Flash.
$rfactor is a constant variable. It corresponds to your map scale. You might have to tweak this value up or down a bit (but it worked for me at 290).
$radBtm = deg2rad(23); the value "23" is the latitude at the bottom of my map.

Getting the x value is much, much easier:
$x =round(((125.49 - $lon)*10.178), 2);

125.59 is the longitude at the top left corner of my map.
$lon is the longitude for the city I want to plot
10.178 is a constant that is based on the width of the map, it's calculated as follows:
longitude West - longitude East / width of map
125.49 - 66.54 = 58.95
600 / 58.95 = 10.178
Let me put it another way: every 10.178 pixels on your map equals one degree of longitude.

Here's how I plotted Detroit:
Detroit, MI<br>
Longitude: 83<br>
Latitude: 42.24<br>
$lon = 83;
$lat = 42.24;
$x =round(((125.49 - $lon)*10.178), 2);
print "x: ".$x."<br>";
print "y: ".findYcoord($lat);      

Works like a champ, I swear by the results and it's far less complicated or confusing than any other solution I've seen thus far.
Thanks again to everyone.
David and Jennifer.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.