I am not sure where the question points to. As far as I understand it, you have the user select an area - which is expressed in geographical coordinates - and then use these coordinates to calculate the tiles to be retrieved.
Under Slippy map tilenames in the OSM wiki there is a very good explanation how to do this:
Reproject the coordinates to the Mercator projection (from EPSG:4326 to EPSG:3857):
x = lon
y = arsinh(tan(lat)) = log[tan(lat) + sec(lat)]
(lat and lon are in radians)
Transform range of x
and y
to 0
– 1
and shift origin to top left corner:
x = [1 + (x / π)] / 2
y = [1 − (y / π)] / 2
- Calculate the number of tiles across the map,
n
, using 2^zoom
- Multiply
x
and y
by n
. Round results down to give tilex
and tiley
.