Skip to main content

Get Routes

curl -X POST https://autonomous.stu.nighthawkcodingsociety.com/api/get_routes \
  -H "Content-Type: application/json" \
  -d '{
    "origin": "Poway High School",
    "destination": "Poway City Hall",
    "mode": "driving"
  }'
Calculates one or more routes between two locations with traffic-adjusted duration estimates.

Request Body

origin
string
required
Starting location (address or place name)
destination
string
required
Destination location (address or place name)
mode
string
default:"driving"
Transportation mode. Options: driving, walking, bicycling, transit

Response

Returns an array of route objects, ordered by preference (fastest route first).
[].geometry
string
Encoded polyline string for rendering the route on a map (Polyline format)
[].total_distance
string
Human-readable total distance (e.g., “3.2 km”)
[].total_duration
string
Estimated duration without traffic (e.g., “8 mins”)
[].traffic_adjusted_duration
string
Duration adjusted for current traffic conditions (e.g., “12 mins”)
[].details
array
Array of turn-by-turn navigation steps

Response Example

[
  {
    "geometry": "uu{eE~kqkU@QCGAG?G@E@C...",
    "total_distance": "3.2 km",
    "total_duration": "8 mins",
    "traffic_adjusted_duration": "12 mins",
    "details": [
      {
        "instruction": "Head north on Poway Rd",
        "distance": "0.5 km",
        "duration": "2 mins"
      },
      {
        "instruction": "Turn right onto Civic Center Dr",
        "distance": "1.2 km",
        "duration": "3 mins"
      }
    ]
  }
]

Real Implementation

From navigation/findBestRoute/map.js:21-73:
document.getElementById('fetch_routes_btn').addEventListener('click', async () => {
  const origin = document.getElementById('origin').value;
  const destination = document.getElementById('destination').value;
  const mode = document.getElementById('mode').value || 'driving';

  if (!origin || !destination) {
    alert('Please enter both origin and destination.');
    return;
  }

  const response = await fetch(apiUrl, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ origin, destination, mode }),
  });

  const routes = await response.json();

  routes.forEach((route, idx) => {
    const header = document.createElement('h4');
    header.textContent = `Route ${idx + 1} - ${route.total_distance} - Est. Time: ${route.traffic_adjusted_duration || route.total_duration}`;

    // Decode and display polyline on map
    if (route.geometry) {
      const decoded = polyline.decode(route.geometry);
      const polylineLayer = L.polyline(decoded, {
        color: idx === 0 ? 'blue' : 'gray',
        weight: 4,
        opacity: 0.8,
      }).addTo(map);
    }
  });
});

Traffic Adjustment Logic

The API calculates traffic_adjusted_duration by analyzing current traffic conditions along the route. The client displays this value when available, falling back to total_duration for real-time estimates.

Polyline Decoding

The geometry field uses the Mapbox Polyline format. Decode it using:
import polyline from '@mapbox/polyline';
const coordinates = polyline.decode(route.geometry);
// Returns array of [lat, lng] pairs

Error Responses

If no routes are found or an error occurs:
{
  "error": "No routes found between the specified locations"
}

Build docs developers (and LLMs) love