16

I have multiple tile sources generated using gdal2tiles I'd like to present on the same map. So when presenting a tile I need to determine which source to serve it up from, checking against the bounds.

Anyone know how to calculate the lat lon bounds for a single tile based purely on zoom, x, and y (from the file structure generated by gdal2tiles)? BTW: I'm using Google Maps API v3 in case there's a need to call some functionality from the API to help with calculations.

The reason I'm restricted to purely zoom, x, and y is because the tiles aren't just getting called by an overlay on the map but also some custom print functionality that allows printing outside of the map.

22

The math is described at:

http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/

…including the source code for command line utility and an online demonstration.

It is also pretty simple math:

function tile2long(x,z) { return (x/Math.pow(2,z)*360-180); }

function tile2lat(y,z) {
    var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
    return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}

Note the difference between XYZ/Google vs TMS in y axis.

Google Maps API V3 gives you the required functions too via .fromPointToLatLng() of map.getProjection().

  • I am just trying to understand the above solution as I have the same problem. Do you mean in above solution I should 1. First convert x,y and zoom to lat/long using tile2long() and tile2lat(). 2. Use lat/long created in step to create bounds? How should I do it? – Vishal May 1 '17 at 6:08
  • @Petr Do you have anything for c++? – Majid Hojati Jan 19 '18 at 10:11
  • it doesn't take tile size into account? – Muhammad Umer Mar 19 '18 at 19:53
  • @petr-pridal This results in a point, to the best of my knowledge a tile is an area or surface. Could you provide code for a bounding box? Thanks in advance. – Herbert Sep 6 '18 at 14:22
  • From this article gist.github.com/tmcw/4954720, TMS is the same tiles, but indexed backwards. This is the conversion formula : Math.floor(Math.pow(2, z) - y - 1) – Haggai Oct 16 '18 at 8:04
0

This OpenStreetMap wiki page might be helpful for other languages as well:

https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Implementations

0

This is my working code:

jsfiddle sample of google image map type overlay

jsfiddle another sample of google overlay image

function tile2long(x,z) {

  return (x/Math.pow(2,z)*360-180);

}





function tile2lat(y,z) {

  var n=Math.PI-2*Math.PI*y/Math.pow(2,z);

  return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));

}





 var x = coord.x;

 var y = coord.y;

 var z = zoom;



 var NW_long = tile2long(x,z);



 var SW_long = tile2long(x,z);

 var SW_lat =  tile2lat(y+1,z);



 var NE_long = tile2long(x+1,z);

  var NE_lat =  tile2lat(y,z);







  var export_bbox = SW_long + ',' + SW_lat + ',' + NE_long + ',' + NE_lat;



     var url_param = '&format=png&size=256,256&transparent=true&f=image&bboxSR=4326&imageSR=4326';





   var url_layers =''





       var root_url = 'https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export?bbox=';



     var tile_url = root_url + export_bbox + url_param + url_layers;

maptiler get bbox from coodinate number and zoom

how to calculate google coordinate number zoom, convert, lat long etc....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.