Need to create bounding boxes for spatial queries or map views? This comprehensive guide covers everything about generating geographic bounding boxes via API, including radius calculations, polygon bounds, and implementation examples.
What is a Bounding Box?
A bounding box (bbox) is the smallest rectangle that completely contains a geographic feature or area. It's defined by four coordinates: minimum latitude, minimum longitude, maximum latitude, and maximum longitude.
Bounding boxes are fundamental to spatial indexing and queries, allowing databases to quickly filter locations within a rectangular region.
Types of Bounding Boxes
Radius-based Bounding Box
A rectangle that contains all points within a specified distance from a center point. Useful for "find nearby" queries.
Polygon Bounding Box
The minimum bounding rectangle for a polygon or set of points. Useful for map viewport calculations.
Buffered Bounding Box
An existing bounding box expanded by a specified distance in all directions.
Using the Bounding Box API
TinyFn provides endpoints to generate bounding boxes:
POST https://api.tinyfn.io/v1/geo/bounding-box
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"center": {"lat": 40.7128, "lng": -74.0060},
"radius": 10,
"unit": "km"
}
{
"bbox": {
"min_lat": 40.6229,
"min_lng": -74.1234,
"max_lat": 40.8027,
"max_lng": -73.8886
},
"bounds": [[40.6229, -74.1234], [40.8027, -73.8886]],
"geojson": {
"type": "Polygon",
"coordinates": [[[...]]]
}
}
Parameters
| Parameter | Type | Description |
|---|---|---|
center |
object | Center point with lat/lng (for radius-based) |
radius |
number | Radius distance (for radius-based) |
unit |
string | Distance unit: "km", "mi", "m", "nm" (default: "km") |
points |
array | Array of coordinates (for polygon bounds) |
buffer |
number | Additional buffer distance to add |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/geo/bounding-box',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
center: { lat: 40.7128, lng: -74.0060 },
radius: 10,
unit: 'km'
})
}
);
const { bbox } = await response.json();
// Use bbox for database query
const query = `
SELECT * FROM locations
WHERE lat BETWEEN ${bbox.min_lat} AND ${bbox.max_lat}
AND lng BETWEEN ${bbox.min_lng} AND ${bbox.max_lng}
`;
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/geo/bounding-box',
headers={'X-API-Key': 'your-api-key'},
json={
'center': {'lat': 40.7128, 'lng': -74.0060},
'radius': 10,
'unit': 'km'
}
)
bbox = response.json()['bbox']
# Use bbox for database query
query = f"""
SELECT * FROM locations
WHERE lat BETWEEN {bbox['min_lat']} AND {bbox['max_lat']}
AND lng BETWEEN {bbox['min_lng']} AND {bbox['max_lng']}
"""
cURL
curl -X POST "https://api.tinyfn.io/v1/geo/bounding-box" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"center": {"lat": 40.7128, "lng": -74.0060},
"radius": 10,
"unit": "km"
}'
Common Use Cases
- Nearby Search: Find all locations within a radius using bbox as initial filter
- Map Viewports: Calculate the bounds for displaying a set of markers
- Spatial Indexing: Create efficient database queries using bbox constraints
- Tile Loading: Determine which map tiles to load for a given view
- Geocoding: Bias search results to a specific geographic area
Best Practices
- Add a buffer: Include a small buffer to handle edge cases
- Use as pre-filter: Bbox is a quick filter; refine with exact distance calculations
- Consider latitude: Bounding boxes stretch at higher latitudes due to longitude convergence
- Handle antimeridian: Special handling needed for boxes crossing the date line
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-geo": {
"url": "https://api.tinyfn.io/mcp/geo/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all geolocation tools available via MCP in our Geolocation MCP Tools for AI Agents guide.
Try the Bounding Box API
Get your free API key and start generating bounding boxes in seconds.
Get Free API Key