57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
const routing = require('../services/routing');
|
|
const logger = require('../utils/logger');
|
|
|
|
async function getDirections(req, res) {
|
|
try {
|
|
const { startLat, startLng, endLat, endLng, profile } = req.query;
|
|
|
|
// Validate required parameters
|
|
if (!startLat || !startLng || !endLat || !endLng) {
|
|
return res.status(400).json({
|
|
error: 'Missing required parameters: startLat, startLng, endLat, endLng'
|
|
});
|
|
}
|
|
|
|
const start = {
|
|
lat: parseFloat(startLat),
|
|
lng: parseFloat(startLng)
|
|
};
|
|
const end = {
|
|
lat: parseFloat(endLat),
|
|
lng: parseFloat(endLng)
|
|
};
|
|
|
|
// Validate coordinates
|
|
if (isNaN(start.lat) || isNaN(start.lng) || isNaN(end.lat) || isNaN(end.lng)) {
|
|
return res.status(400).json({ error: 'Invalid coordinates' });
|
|
}
|
|
|
|
// Validate profile
|
|
const validProfiles = ['driving', 'walking', 'cycling'];
|
|
const routeProfile = validProfiles.includes(profile) ? profile : 'driving';
|
|
|
|
const directions = await routing.getDirections(
|
|
start.lat, start.lng,
|
|
end.lat, end.lng,
|
|
routeProfile
|
|
);
|
|
|
|
res.json({
|
|
success: true,
|
|
profile: routeProfile,
|
|
route: directions
|
|
});
|
|
|
|
} catch (error) {
|
|
logger.error('Directions request failed', { error: error.message });
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getDirections
|
|
};
|