convert from tile coordinates to lat/lng

5986 views
Skip to first unread message

spiderplant0

unread,
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:
http://code.google.com/apis/maps/documentation/javascript/reference.html#MapCanvasProjection
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

unread,
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

unread,
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

unread,
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

unread,
Dec 6, 2010, 8:52:42 AM12/6/10
to Google Maps JavaScript API v3
http://code.google.com/apis/maps/documentation/javascript/maptypes.html#WorldCoordinates

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:

http://code.google.com/apis/maps/documentation/javascript/reference.html#Projection

"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:

google.maps.event.addListenerOnce(map, 'projection_changed', function()
{
// the map's projection object can now be used
var projection=map.getProjection() ;
});

Martin.

spiderplant0

unread,
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

unread,
Nov 10, 2011, 9:16:41 AM11/10/11
to google-map...@googlegroups.com
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

unread,
Nov 10, 2011, 3:10:04 PM11/10/11
to google-map...@googlegroups.com
Here are the formulae you'll need.

https://groups.google.com/group/Google-Maps-API/browse_thread/thread/348098f70725fc96/7a0aba451045ed94?lnk=gst&q=author%3Ajgeerdes#7a0aba451045ed94

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

For more information or a project quote:

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.
To view this discussion on the web visit https://groups.google.com/d/msg/google-maps-js-api-v3/-/mAZnp5QXMy0J.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Aaron Edwards

unread,
Oct 17, 2018, 6:33:30 PM10/17/18
to [deprecated] Google Maps JavaScript API v3. Please use the latest post.
Man, it would be great if people would post their code solution, and not just say thanks:

    _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;
   }

Reply all
Reply to author
Forward
0 new messages