curl --request POST \
--url http://localhost:8080/route \
--header 'Content-Type: <content-type>' \
--data '
{
"from": {
"lat": 123,
"lng": 123
},
"to": {
"lat": 123,
"lng": 123
}
}
'{
"route": [
{
"id": 123,
"branch_id": 123,
"name": "<string>",
"latitude": 123,
"longitude": 123
}
]
}Calculate the optimal route between two geographic coordinates
curl --request POST \
--url http://localhost:8080/route \
--header 'Content-Type: <content-type>' \
--data '
{
"from": {
"lat": 123,
"lng": 123
},
"to": {
"lat": 123,
"lng": 123
}
}
'{
"route": [
{
"id": 123,
"branch_id": 123,
"name": "<string>",
"latitude": 123,
"longitude": 123
}
]
}/route endpoint calculates the optimal path between two geographic coordinates using the available bus routes and walking connections. The algorithm considers walking distance, bus routes, and transfers to find the best route.
POST /route
application/json{
"from": {
"lat": 40.7128,
"lng": -74.0060
},
"to": {
"lat": 40.7580,
"lng": -73.9855
}
}
[
{
"id": 15,
"branch_id": 2,
"name": "Washington Square",
"latitude": 40.7308,
"longitude": -73.9973
},
{
"id": 16,
"branch_id": 2,
"name": "Union Square",
"latitude": 40.7359,
"longitude": -73.9911
},
{
"id": 23,
"branch_id": 3,
"name": "Grand Central",
"latitude": 40.7527,
"longitude": -73.9772
}
]
{
"error": "No hay rutas disponibles"
}
| Error Message | Meaning |
|---|---|
"No existen paradas para empezar" | No bus stops within walking distance of origin |
"No hay rutas disponibles" | No valid route found between the coordinates |
"Error message from service" | External service error |
curl -X POST http://localhost:8080/route \
-H "Content-Type: application/json" \
-d '{
"from": {
"lat": 40.7128,
"lng": -74.0060
},
"to": {
"lat": 40.7580,
"lng": -73.9855
}
}'
src/server/server.js:27-38):
app.post("/route", (req, resp) => {
const routeData = req.body
ServerMockup()
.then(busStopsData => {
const route = findLogic.findRoute(busStopsData, routeData)
resp.json(route)
})
.catch((err) => {
console.error(err)
resp.json({ error: err.message })
})
})
src/server/FindLogic.js:42) uses several sophisticated techniques:
src/server/FindLogic.js:34-38):
| Parameter | Value | Description |
|---|---|---|
walkKmh | 1 km/h | Walking speed for time calculations |
busKmh | 60 km/h | Average bus travel speed |
busWaitH | 0.5 hours | Expected wait time at stops |
maxWalkKm | 0.8 km | Maximum walking distance to/from stops |
from and to coordinatesServerMockup()findLogic.findRoute() calculates optimal path:
async function calculateRoute(from, to) {
const response = await fetch('http://localhost:8080/route', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ from, to })
});
const result = await response.json();
if (result.error) {
// Handle different error types
if (result.error.includes('paradas para empezar')) {
return {
success: false,
message: 'No bus stops found near your location. Try a different starting point.'
};
} else if (result.error.includes('No hay rutas')) {
return {
success: false,
message: 'No route available between these locations. Try different points.'
};
} else {
return {
success: false,
message: 'Unable to calculate route. Please try again.'
};
}
}
return {
success: true,
route: result
};
}
async function showRouteOnMap(fromCoords, toCoords) {
const response = await fetch('http://localhost:8080/route', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ from: fromCoords, to: toCoords })
});
const route = await response.json();
if (!route.error) {
// Draw route line on map
const coordinates = route.map(stop => [stop.latitude, stop.longitude]);
drawPolyline(coordinates);
// Add markers for each stop
route.forEach((stop, index) => {
addMarker({
lat: stop.latitude,
lng: stop.longitude,
title: `${index + 1}. ${stop.name}`,
icon: 'bus-stop'
});
});
}
}
function analyzeRoute(route) {
// Count unique bus routes used
const uniqueRoutes = new Set(route.map(stop => stop.branch_id));
const transfers = uniqueRoutes.size - 1;
// Calculate total distance
let totalDistance = 0;
for (let i = 0; i < route.length - 1; i++) {
totalDistance += calculateDistance(
route[i].latitude, route[i].longitude,
route[i + 1].latitude, route[i + 1].longitude
);
}
return {
stops: route.length,
transfers: transfers,
distance: totalDistance.toFixed(2) + ' km',
routes: Array.from(uniqueRoutes)
};
}
// React component example
function RoutePlanner() {
const [route, setRoute] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const findRoute = async (from, to) => {
setLoading(true);
setError(null);
try {
const response = await fetch('http://localhost:8080/route', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ from, to })
});
const data = await response.json();
if (data.error) {
setError(data.error);
} else {
setRoute(data);
}
} catch (err) {
setError('Network error. Please try again.');
} finally {
setLoading(false);
}
};
return (
<div>
{loading && <Spinner />}
{error && <ErrorMessage>{error}</ErrorMessage>}
{route && <RouteDisplay stops={route} />}
</div>
);
}