Converting long/lat to pixel x/y, given a zoom-level

.net, coordinates, google-maps, maps, pixel

Solution

"If it is a Mercator projection, you shouldn't need to worry about the curvature of the earth since all of the lat/lon lines are at equal spacing"

Perhaps you're thinking of the Geographic (aka Plate Carree) projection? The Mercator projection does have equally spaced lines of longitude, but does not have equally spaced lines of latitude (lat = atan(sinh(y)), so 90° is at infinity).

BTW, the math for the Mercator projection on a sphere is here, but if Google Maps is using the WGS84 ellipsoid and you need to get it exact it gets more complicated. In that case, I'd look at this, but beware: it's not for the faint of heart.

Problem

I'm trying to develop a page in ASP.NET that will act as a tile-server for a Google Map It will pull a collection of latitude/longitude points from the database, then render them as small red dots on a transparent background, given a zoom-level (default: 15). It will then return the result as a GIF-type image. Have any algorithms or libraries been developed that allow me to take this set of latitudes/longitudes and convert them to a set of 2D pixel co-ordinates, given a zoom level? (This is all being done server-side, so I can't use the Google Maps API.) Update: Found a code-sample in Perl that does something similar: http://blog.barros.ws/2009/03/06/convert-lat-lng-and-zoom-values-to-pixel-xy-on-a-map/ Trouble is, I don't know Perl, and don't really have the time to crack open a book and learn it. Can anyone help me to decipher what's going on in this function? ``` sub Google_Coord_to_Pix { my $value = shift ; my $lat = shift ; my $lng = shift ; my @d = ( ) ; my $e = 0 ; $d[1] = sprintf("%0.0f", $$value{'bmO'} + $lng * $$value{'pixLngDeg'} ) ; $e = sin($lat * $$value{'Wa'}) ; if( $e > 0.99999 ) { $e = 0.99999 ; } if( $e < -0.99999 ) { $e = -0.99999 ; } $d[0] = sprintf("%0.0f", $$value{'bmO'} + 0.5 * log((1 + $e) / (1 - $e)) * (-1) * $$value{'pixLngRad'} ) ; return (@d) ; } ```

Original source