# Finding the Map-Tiles(z,x,y,tile_data) in a given Bounding box and zoom level

I have this information:For Ogun State Nigeria from http://www.maphill.com/nigeria/ogun/

``````South
6° 18' 6" N
North
7° 59' 23" N
West
2° 40' 6" E
East
4° 34' 47" E
``````

I have map tiles @ different zoom levels 10,11,12,13,14,etc for the whole of Nigeria stored as z,x,y,tile_data

Please how can I identify just the tiles within the given bounds at each zoom level

First You can check this page as a guide to you Tile Calculator BETA

But you need to know that you will create a server side page that will handle the requested Bounding box (BBOX) and the required zoom level and will response with Tiles (X,Y) coordinates as the Slippy map tilenames and as this page Tiles à la Google Maps: Coordinates, Tile Bounds and Projection.

As an example i made a php sample code to get the starting Tile(X,Y) and the last Tile(X,Y) from both of them you can get total number of the required tiles in each row and column and also you can get the total number of tiles from the simple formula (#Rows x #Columns).

``````<?php

/* Usage:
http://localhost/tms/index.php?z={z}&minlon={minlon}&minlat={minlat}&maxlon={maxlon}&maxlat={maxlat}
http://localhost/tms/index.php?z=10&minlon=26.10&minlat=28.90&maxlon=28.40&maxlat=28.94
*/

if (empty (\$_GET['minlon'])){
\$minLon = 26.10;
}else{
\$minLon = \$_GET['minlon'];
}
if (empty (\$_GET['maxlon'])){
\$maxLon =  28.40;
}else{
\$maxLon = \$_GET['maxlon'];
}
if (empty (\$_GET['minlat'])){
\$minLat = 28.90;
}else{
\$minLat = \$_GET['minlat'];
}
if (empty (\$_GET['maxlat'])){
\$maxLat = 28.94;
}else{
\$maxLat = \$_GET['maxlat'];
}
if (empty (\$_GET['z'])){
\$z = 14;
}else{
\$z = \$_GET['z'];
}

function degTorad(\$deg){return \$deg * M_PI / 180;}
function lonToTileX(\$lon, \$zoom){return floor(((\$lon + 180) / 360) * pow(2, \$zoom));}
function latToTileY(\$lat, \$zoom){return floor((1 - log(tan(degTorad(\$lat)) + 1 / cos(degTorad(\$lat))) / M_PI) / 2 * pow(2, \$zoom));}

\$minTileX = lonToTileX(\$minLon,\$z);
\$maxTileX = lonToTileX(\$maxLon,\$z);
\$minTileY = latToTileY(\$maxLat,\$z);
\$maxTileY = latToTileY(\$minLat,\$z);

//echo nl2br("Min:Max LON= " . \$minLon . ":" . \$maxLon . "\r\n" . "Min:Max LAT=" . \$minLat . ":" . \$maxLat . "\r\n");
echo nl2br('Min:Max X= ' . \$minTileX . ' : ' . \$maxTileX  . '  - #of Tiles\Row: ' . ((\$maxTileX - \$minTileX)+1) . "\r\n" . 'Min:Max Y= ' . \$minTileY . ' : ' . \$maxTileY . '  - #of Tiles\Col: ' . ((\$maxTileY - \$minTileY)+1));
?>
``````

If you use the sample url parameters:

``````http://localhost/tms/index.php?z=10&minlon=26.10&minlat=28.90&maxlon=28.40&maxlat=28.94
``````

The output will be:

``````Min:Max X= 9379 : 9484 - #of Tiles\Row: 105
Min:Max Y= 6815 : 6817 - #of Tiles\Col: 2
``````

You can test the code online in phpFiddle.org

So if you need to get all the required tiles name you need to loop through the tiles using Tile(X,Y) and change the X and Y values with the (MinTileX to MaxTileX) and from (MinTileY to MaxTileY).

``````For (x=MinTileX to MaxTileX) Then
For (y=MinTileY to MaxTileY) Then
tileUrl = "http://Your-Tile-Server-URL/{Z}/{X}/{Y}.png"  // or whatever your Tile server URL schemas.