0
votes

On the link below [1] I can see how to get the URL of the tile for specific latlon.

var zoom = 15;
var lat = 47;
var lon = 8;
        
function long2tile(lon,zoom) { return (Math.floor((lon+180)/360*Math.pow(2,zoom))); }
function lat2tile(lat,zoom)  { return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom))); }
        
var x = long2tile(lon, zoom); // get x long2tile
var y = lat2tile(lat, zoom);  // get y lat2tile
        
tileUrl = "tile.osm.org/" + zoom + "/" + x + "/" + y + ".png"

What I want to know is how to get all the tile URL's for the surrounding tiles. Usually there is a grid of say 3x3 tiles and I'm looking in the one in the middle, that would be my base latlon. How do I get URL's for all the other neighboring tiles?

[1] http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#ECMAScript_.28JavaScript.2FActionScript.2C_etc..29

1
Modified your code to work for x & y. - Yozef

1 Answers

3
votes

These are just coordinates in a grid. Increase/decrease your x and y coordinates accordingly to get all surrounding tiles. Example:

enter image description here

Quoting from the Wiki:

  • X goes from 0 (left edge is 180 °W) to 2^zoom − 1 (right edge is 180 °E)
  • Y goes from 0 (top edge is 85.0511 °N) to 2^zoom − 1 (bottom edge is 85.0511 °S) in a Mercator projection

For the curious, the number 85.0511 is the result of arctan(sinh(π)). By using this bound, the entire map becomes a (very large) square.