Reputation: 620
I am using these two function to convert latlng to tile co-ordinates.what i am unable to understand is if at zoom level 1 there are 4 tiles. So of which tiles these co-ordinates are belong to? Or these belongs to left-top?
public int langtoTileX(Double lng,int zoom)
{
double x;
x = Math.round((256*(Math.pow(2,zoom-1)))+(lng*((256*(Math.pow(2,zoom)))/360)));
return ((int)(x/256));
}
public int lattoTileY(Double lat,int zoom)
{
Double exp = Math.sin(lat*Math.PI/180);
if (exp < -.9999)
exp = -.9999;
if (exp > .9999)
exp = .9999 ;
return (int) (Math.round (256*(Math.pow(2,zoom-1)))+((.5*Math.log((1+exp)/(1-exp)))*((-256*(Math.pow(2,zoom)))/(2*Math.PI))))/256;
}
The same type of formula's are used in this example:
https://developers.google.com/maps/documentation/javascript/examples/map-coordinates
Upvotes: 2
Views: 3695
Reputation: 12592
Most likely its top-left tile. When you want to subdivide a map it can be useful to use a quadtree hence there is 4 tiles at zoom level 1. Look for the bing maps quadkey solution: http://msdn.microsoft.com/en-us/library/bb259689.aspx. But Google maps uses a x,y pair for the tiles: How can I get Tile Count, Tile X, Tile Y details without specifying zoom Level (or LevelOfDetails)?. In zoom level 0 there is 1 tile at zoom level 1 you can see 4 tiles and some tiles on both side, etc. pp. Link: http://facstaff.unca.edu/mcmcclur/GoogleMaps/Projections/GoogleCoords.html, http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection.
Update: To get the bounding box from x,y pair use a rectangle like this x,x+1,y,y+1: TileProvider method getTile - need to translate x and y to lat/long. But you can also convert between Bing and Google maps. Substitute the quadkey values as follows: 0=q, 1=r, 2=t, 3=s (?), maybe also 0=q, 1=r, 2=s, 3=t (?) and append it to the url http://kh.google.com/kh?v=3&;t=quadkey. Source: http://dzone.com/snippets/converting-between-2-google.
Update2: The url http://kh.google.com is dead but you can still use the snippet:
def quadtree(x,y, zoom):
out = []
m = {(0,0):'q', (0,1):'t', (1,0):'r', (1,1):'s'}
for i in range(17-zoom):
x, rx = divmod(x, 2)
y, ry = divmod(y, 2)
out.insert(0, m[(rx,ry)])
return 't' + ''.join(out)
def xyzoom(quad):
x, y, z = 0, 0, 17
m = {'q':(0,0), 't':(0,1), 'r':(1,0), 's':(1,1)}
for c in quad[1:]:
x = x*2 + m[c][0]
y = y*2 + m[c][1]
z -= 1
return x, y, z
In the map the curve start at the top-right corner to bottom-right to bottom-left to top-left. Hence it looks like a reverse U-shape? Maybe I'm wrong and it's top-left, top-right, bottom-left, bottom-right? Then it's a Z-shape. Then the lat,lng pair is in the top-left tile?
To get the bounding box, I assume that long and lat are given in seconds.
First, you need to calculate width of the rectangle in seconds. One second is 30.9 meters on the equator, for other latitudes, multiply by cos(lat), so to convert it to seconds you do the following:
double widthSeconds = meters / (30.9 * cos(lat));
Second, knowing the center of the box, it's easy to calculate the coordinates of corners:
1 BoundingBox around a geo coordinate
Upvotes: 4