# convert from tile coordinates to lat/lng

7,224 views

### spiderplant0

Dec 5, 2010, 11:38:30 AM12/5/10
to Google Maps JavaScript API v3
Hi, my website needs to convert google maps tile cordinates (x, y and
zoom) into LatLngBounds (latitude and longitude for the north-east and
south west corners of the tile).
For example, for the tile showing Scotland: (x,y) = (7,4), zoom=4
The bounds are: NE=(66.51326044311185, 0) SW=(55.7765730186677, -22.5)
Is there any V3 Google Maps Javascript that can do this conversion?
I found some likely functions here:
But I cant figure out how to get it to work.
In case its relevant, I need this for my implementation of the
getTileUrl(point, zoom) method for the ImageMapType class.
Thanks

### Esa

Dec 5, 2010, 9:36:16 PM12/5/10
to Google Maps JavaScript API v3
The projection conversion methods of API are based on World
Coordinates. Those are pixel coordinates in 256 x 256 space (Zoom
level 0). Coordinates are floating point numbers.

### spiderplant0

Dec 6, 2010, 7:38:23 AM12/6/10
to Google Maps JavaScript API v3
er, not sure how this helps.
Are you saying it cant be done?
Or is my question not clear?

### spiderplant0

Dec 6, 2010, 8:23:02 AM12/6/10
to Google Maps JavaScript API v3
I'll clarify the question.
I need to convert tile coords plus zoom into lat/lng.
I can easily convert tile coords plus zoom into and world coordinates
as the maths is simple.
However what is not trivial is the conversion between world
coordinates and lat/lng.
Does google maps include a function to do this?

### Martin

Dec 6, 2010, 8:52:42 AM12/6/10
to Google Maps JavaScript API v3

Check out the World Coordinates and Pixel Coordinates sections.

pixelCoordinate = worldCoordinate * (2 to the power of zoomLevel)

So you want to convert your tile coordinates into two pixel
coordinates, pixel (x,y) for both sw corner and ne corner.

Now convert those pixel coordinates to world coordinates:

world coordinates = pixel coordinates / (2 to the power of zoom level)

(I think that's right!?)

And finally convert world coordinates to (lat, lng) using the map
projection's fromPointToLatLng() method:

"This interface specifies a function which implements translation from
world coordinates on a map projection to LatLng values. The Maps API
calls this method when it needs to translate actions on screen to
positions on the map. Projection objects must implement this method."

Note:

If map.getProjection() returns undefined then you're trying to access
the projection before the map has fully initialised.

A solution is:

{
// the map's projection object can now be used
var projection=map.getProjection() ;
});

Martin.

### spiderplant0

Dec 6, 2010, 4:40:18 PM12/6/10
to Google Maps JavaScript API v3
Thanks Martin, thats just what I was looking for.

### BigLittleFlan

Nov 10, 2011, 9:16:41 AM11/10/11
Hi,

I've got the exact same problem you were having, would you be able to post the code you used to solve this?

Thanks

### Jeremy Geerdes

Nov 10, 2011, 3:10:04 PM11/10/11
Here are the formulae you'll need.

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
`    _getWmsTileBoundingBox(tileCoordinates, gmap) {        let tileSize = 256;        // first convert tile coordinates to pixel coordinates for NW and SE corners of tile        let nwPixelX = tileCoordinates.x * tileSize;        let nwPixelY = tileCoordinates.y * tileSize;        let sePixelX = (tileCoordinates.x + 1)  * tileSize - 1;        let sePixelY = (tileCoordinates.y + 1)  * tileSize - 1;        // next convert pixel coordinates to world (web mercator) coodinates        let nwWorldX = nwPixelX / (Math.pow(2, zoom));        let nwWorldY = nwPixelY / (Math.pow(2, zoom));        let seWorldX = sePixelX / (Math.pow(2, zoom));        let seWorldY = sePixelY / (Math.pow(2, zoom));        let nwWorldPoint = new google.maps.Point(nwWorldX, nwWorldY);        let seWorldPoint = new google.maps.Point(seWorldX, seWorldY);        // finally use Google Maps' native method to convert world coordinates to Lat/Lng coordinates, and return a bounding box        let nwLatLng = gmap.getProjection().fromPointToLatLng(nwWorldPoint);        let seLatLng = gmap.getProjection().fromPointToLatLng(seWorldPoint);        let bbox = nwLatLng.lng() + ',' + seLatLng.lat() + ',' + seLatLng.lng() + ',' + nwLatLng.lat();        return bbox;    }`