Computing which tiles are in view within a given bounds.

24 views
Skip to first unread message

Garthan

unread,
Oct 26, 2011, 5:34:44 PM10/26/11
to google-map...@googlegroups.com
I have data which is stored in a format whose file names mirrors the tiles used by google maps
I needed to compute the rows and columns of tiles that are in view with a given bounds and zoom level

In version 2 of the API I computed them using this function

    var sw = proj.fromLatLngToPixel(bounds.getSouthWest(), Zoom);
    var ne = proj.fromLatLngToPixel(bounds.getNorthEast(), Zoom);
   
    var startcol = parseInt(sw.x / 256 - buffer);
    var endcol = parseInt(ne.x / 256 + buffer);
    var endrow = parseInt(sw.y / 256 + buffer);
    var startrow = parseInt(ne.y / 256 - buffer);

I am trying to figure out how to get this for v3

Martin™

unread,
Oct 26, 2011, 8:05:05 PM10/26/11
to Google Maps JavaScript API v3
Hi.

This will get the tile that myLatLng is on:

var zoom=myMap.getZoom();
var worldPoint=myMap.getProjection().fromLatLngToPoint(myLatLng);
var pixelPoint=new google.maps.Point(parseInt(worldPoint.x*Math.pow(2,
zoom)), parseInt(worldPoint.y*Math.pow(2, zoom)));

var tileX=parseInt(pixelPoint.x/256);
var tileY=parseInt(pixelPoint.y/256);

Martin.

Garthan

unread,
Oct 27, 2011, 9:56:17 AM10/27/11
to google-map...@googlegroups.com
Thanks great that ought to do the trick. I was considering whether I needed to use getWorldWidth offset by 90 or a 180 degrees, then use the ratio and so on... ... from LatLngToPoint was what I was looking for.
Reply all
Reply to author
Forward
0 new messages