AS3 latitude and longitude positioning in a Mercator and Equirectangular projection

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 Projection

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:

  1. public function mercator_position(lat:Number, lng:Number):Point{
  2.   // longitude: just scale
  3.   var newX:Number = (map_width * (180 + lng) / 360) % map_width;
  5.   // latitude: using the Mercator projection
  6.   var radlat:Number = lat * Math.PI / 180;  // convert from degrees to radians
  7.   var newY:Number = Math.log(Math.tan((radlat/2) + (Math.PI/4)));  // do the Mercator projection (w/ equator of 2pi units)
  8.   newY = (map_height / 2) - (map_width * newY / (2 * Math.PI));   // fit it to our map   
  10.   return new Point(newX,newY);
  11. }

Mercator Projection

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 de-facto 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:

  1. public function equirectangular_position(lat:Number, lng:Number):Point{
  2.   var newX:Number = ((lng + 180) * (map_width  / 360));
  3.   var newY:Number = (((lat * -1) + 90) * (map_height  / 180));
  5.   return new Point(newX,newY);
  6. }

To test all this I made a little demo.

3 Comment(s)

  1. 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 X-Y plane, the region being defined by east and west limits of latitude and east and west limits longitude?


  2. On Sep 13, 2011, ramindeja said:


    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.


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

Post a Comment