Need to find the center point between two or more geographic locations? This comprehensive guide covers everything you need to know about calculating geographic midpoints via API, including the mathematics behind it, practical use cases, and implementation examples.
What is a Geographic Midpoint?
A geographic midpoint is the center of gravity for a set of locations on Earth. Unlike a simple arithmetic average, it accounts for the spherical nature of the Earth, ensuring accurate results even for distant locations.
For two points, the midpoint lies exactly halfway along the great-circle path between them. For multiple points, it represents the centroid that minimizes total distance to all locations.
How Midpoint Calculation Works
The geographic midpoint calculation involves several steps:
Spherical Coordinate Conversion
Latitude and longitude are converted to 3D Cartesian coordinates (x, y, z) on a unit sphere. This allows for proper averaging that respects Earth's curvature.
Vector Averaging
The Cartesian coordinates are averaged, and the resulting vector is normalized and converted back to latitude/longitude.
Using the Midpoint Calculator API
TinyFn provides a simple endpoint to calculate geographic midpoints:
POST https://api.tinyfn.io/v1/geo/midpoint
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"points": [
{"lat": 40.7128, "lng": -74.0060},
{"lat": 34.0522, "lng": -118.2437}
]
}
{
"midpoint": {
"lat": 37.6688,
"lng": -97.3375
},
"input_points": 2
}
Parameters
| Parameter | Type | Description |
|---|---|---|
points |
array | Array of coordinate objects with lat/lng properties (required) |
weights |
array | Optional weights for each point (default: equal weights) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/geo/midpoint',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
points: [
{ lat: 40.7128, lng: -74.0060 }, // New York
{ lat: 34.0522, lng: -118.2437 } // Los Angeles
]
})
}
);
const { midpoint } = await response.json();
console.log(midpoint); // { lat: 37.6688, lng: -97.3375 }
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/geo/midpoint',
headers={'X-API-Key': 'your-api-key'},
json={
'points': [
{'lat': 40.7128, 'lng': -74.0060}, # New York
{'lat': 34.0522, 'lng': -118.2437} # Los Angeles
]
}
)
midpoint = response.json()['midpoint']
print(midpoint) # {'lat': 37.6688, 'lng': -97.3375}
cURL
curl -X POST "https://api.tinyfn.io/v1/geo/midpoint" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"points": [
{"lat": 40.7128, "lng": -74.0060},
{"lat": 34.0522, "lng": -118.2437}
]
}'
Common Use Cases
- Meeting Point Planning: Find a fair meeting location between multiple people
- Logistics Optimization: Determine optimal warehouse or distribution center locations
- Business Site Selection: Identify central locations for serving multiple markets
- Travel Planning: Find central points for road trips with multiple destinations
- Emergency Services: Optimize placement of emergency response centers
Best Practices
- Use weighted midpoints: When locations have different importance (e.g., population), use weights
- Validate coordinates: Ensure all coordinates are valid before making API calls
- Consider context: The geographic midpoint may be in water or inaccessible terrain
- Batch requests: Calculate midpoints for multiple sets of points in a single request when possible
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 Midpoint Calculator API
Get your free API key and start calculating geographic midpoints in seconds.
Get Free API Key