# Debugging an application that uses Google Maps

Thanks to Shuja and Guillaume that are kind enough to let me bug them all the time, I managed to finish the module to position the message on the map using latitude and longitude. It was hard because I had to play with three different reference systems: the canvas, the tile and finally the lat-long Mercator Projection.

To resources contributed to this little success: the first is this web site that allows you to debug a google map application, revealing the tile indexing system and the coordinates references. However, mind that the code offered as example is faulty. On a newsgroup I managed to find the good code that I am posting below. The code allow to convert a Google Map tile index, to latitude and longitude. Additionally, I converted the javascript into python 🙂 Tags: , , ,

def _inv_project(self, x, y, zoom):

“””returns a Rectangle2D with x = lon degrees , y = lat degrees,

width=lonSpan, height=latSpan for an x,y,zoom as used by google.

“””

lon = -180.0 # x

lonWidth = 360.0 # width 360

# double lat = -90;  // y

# double latHeight = 180; // height 180

lat      = -1.0

latHeight = 2.0

tilesAtThisZoom = 1 << (17 – zoom)

lonWidth  = 360.0 / tilesAtThisZoom

lon      = -180.0 + (x * lonWidth)

latHeight = -2.0 / tilesAtThisZoom

lat      = 1.0 + (y * latHeight)

# convert lat and latHeight to degrees in a mercator projection

# note that in fact the coordinates go from

# about -85 to +85 not -90 to 90!

latHeight += lat

latHeight = (2.0 * math.atan(math.exp(math.pi * latHeight))) – (math.pi / 2.0)

latHeight *= (180.0 / math.pi);

lat = (2.0 * math.atan(math.exp(math.pi * lat))) – (math.pi / 2.0)

lat *= (180.0 / math.pi)

latHeight -= lat

if (lonWidth < 0.0):

lon      = lon + lonWidth

lonWidth = -lonWidth

if (latHeight < 0.0):

lat      = lat + latHeight

latHeight = -latHeight

return (lon, lat, lonWidth, latHeight)