Need to calculate the direction between two geographic points? This guide covers everything you need to know about calculating navigation bearings programmatically via API, including initial and final bearings, use cases, and implementation examples in multiple languages.
What is a Bearing?
A bearing is the compass direction from one point to another, measured in degrees from north (0/360 degrees), clockwise through east (90), south (180), and west (270). Bearings are essential for navigation, mapping, and location-based applications.
For example, the bearing from New York to London is approximately 51 degrees (roughly northeast).
Types of Bearings
When calculating bearings on a sphere, there are two important types:
Initial Bearing (Forward Azimuth)
The direction you need to face when starting your journey. This is what most applications need.
Final Bearing
The direction you'll be facing when you arrive, following a great circle path. Different from initial bearing due to Earth's curvature.
Reverse Bearing
The bearing from destination back to origin. Not simply initial bearing + 180 degrees due to Earth's curvature.
Using the Bearing Calculator API
TinyFn provides a simple endpoint to calculate bearings:
GET https://api.tinyfn.io/v1/geo/bearing?lat1=40.7128&lng1=-74.0060&lat2=51.5074&lng2=-0.1278
Headers: X-API-Key: your-api-key
{
"from": {
"latitude": 40.7128,
"longitude": -74.006
},
"to": {
"latitude": 51.5074,
"longitude": -0.1278
},
"initialBearing": 51.21,
"finalBearing": 107.85,
"distance": 5570.48,
"distanceUnit": "km",
"compassDirection": "NE"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
lat1 |
number | Starting point latitude (-90 to 90) |
lng1 |
number | Starting point longitude (-180 to 180) |
lat2 |
number | Destination latitude (-90 to 90) |
lng2 |
number | Destination longitude (-180 to 180) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/geo/bearing?lat1=40.7128&lng1=-74.0060&lat2=51.5074&lng2=-0.1278',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const { initialBearing, compassDirection, distance } = await response.json();
console.log(`Bearing: ${initialBearing}° (${compassDirection})`);
console.log(`Distance: ${distance} km`);
// Bearing: 51.21° (NE)
// Distance: 5570.48 km
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/geo/bearing',
params={
'lat1': 40.7128, 'lng1': -74.0060,
'lat2': 51.5074, 'lng2': -0.1278
},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(f"Bearing: {data['initialBearing']}° ({data['compassDirection']})")
print(f"Distance: {data['distance']} km")
# Bearing: 51.21° (NE)
# Distance: 5570.48 km
cURL
curl "https://api.tinyfn.io/v1/geo/bearing?lat1=40.7128&lng1=-74.0060&lat2=51.5074&lng2=-0.1278" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Navigation Apps: Show direction to destinations on maps
- Drone/Robot Control: Calculate heading for autonomous navigation
- AR Applications: Point to real-world locations in augmented reality
- Weather/Wind: Display wind direction relative to user location
- Fitness Apps: Show direction to next waypoint on routes
Best Practices
- Use initial bearing for navigation: This is the direction to start traveling
- Consider magnetic declination: For compass use, adjust for local magnetic variation
- Handle edge cases: Same point returns undefined bearing; poles are special cases
- Display compass directions: N, NE, E, SE, etc. are more user-friendly than degrees
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 Bearing Calculator API
Get your free API key and start calculating bearings in seconds.
Get Free API Key