AS3 latitude and longitude positioning in a Mercator and Equirectangular projection
By Gerben Robijn on Aug 30, 2010 in Actionscript 3, Featured, Flash
For a project for thefactor.e I was playing around with placing latitude / longitude coordinates to x y positions and as easy as this sounds it is quite a search through different type of projections and formulas. This post will sum up two of those projections which are mostly used, a mercator and a equirectangular projection, with the as3 codes behind them. To illustrate it all I made a little demo.
Mercator
The Mercator projection is a cylindrical map projection presented by the Belgian, Flemish geographer and cartographer Gerardus Mercator, in 1569. It became the standard map projection for nautical purposes because of its ability to represent lines of constant course, known as rhumb lines or loxodromes, as straight segments. While the linear scale is constant in all directions around any point, thus preserving the angles and the shapes of small objects (which makes the projection conformal), the Mercator projection distorts the size and shape of large objects, as the scale increases from the Equator to the poles, where it becomes infinite. (from Wiki)
In my opinion the best looking projection to use but it also requires the Gudermannian function to calculate the x and y positions. In as3 it will come to this:

public function mercator_position(lat:Number, lng:Number):Point{

// longitude: just scale

var newX:Number = (map_width * (180 + lng) / 360) % map_width;


// latitude: using the Mercator projection

var radlat:Number = lat * Math.PI / 180; // convert from degrees to radians

var newY:Number = Math.log(Math.tan((radlat/2) + (Math.PI/4))); // do the Mercator projection (w/ equator of 2pi units)

newY = (map_height / 2)  (map_width * newY / (2 * Math.PI)); // fit it to our map


return new Point(newX,newY);

}
Equirectangular
The equirectangular projection is a very simple map projection attributed to Marinus of Tyre, who Ptolemy claims invented the projection about AD 100.[1] The projection maps meridians to equally spaced vertical straight lines, and circles of latitude to evenly spread horizontal straight lines. The projection is neither equal area nor conformal. Because of the distortions introduced by this projection, it has little use in navigation or cadastral mapping and finds its main use in thematic mapping. In particular, the plate carrée has become a defacto standard for computer applications that process global maps, such as Celestia and NASA World Wind, because of the particularly simple relationship between the position of an image pixel on the map and its corresponding geographic location on Earth. (from Wiki)
This one is far easier then the Mercator but ain't that good looking. The as3 code for this is:

public function equirectangular_position(lat:Number, lng:Number):Point{

var newX:Number = ((lng + 180) * (map_width / 360));

var newY:Number = (((lat * 1) + 90) * (map_height / 180));


return new Point(newX,newY);

}
To test all this I made a little demo.
On Jul 24, 2011, Jim Howard said:
This is a great help to me on a project I'm working on thanks!
One question. How do I adjust the scaling if I only want to project a region of the world on to an XY plane, the region being defined by east and west limits of latitude and east and west limits longitude?
THANKS!!!!!
On Sep 13, 2011, ramindeja said:
Hi,
I just came across your article and tried to use your mercator formula but I think I'm not using properly.
I have a map of US only that is taken from Goole map with a series of LON, LAT of different location in US.
Using either formula never gives the proper XY on the map. I tried New York and LA and they're quite off.
I would really appreciate your help.
Cordially,
Ramindeja
On Jan 30, 2012, Nelson Mendes said:
Heya, after some searching I stumbled on your great post. Thanks for the code and example. I spotted an issue with the exemple, it doesn't allow for negative values for the latitude and longitude coordinates.