feat(travel): implement Mapbox route recreation using Mapbox Directions API
This commit is contained in:
@@ -49,16 +49,52 @@ function loadTollStations() {
|
|||||||
// ─────────────────────────────────────────────
|
// ─────────────────────────────────────────────
|
||||||
async function getUlysTollLegs(encodedPolyline) {
|
async function getUlysTollLegs(encodedPolyline) {
|
||||||
try {
|
try {
|
||||||
const url = 'https://api-ulys.azure-api.net/placemark/v2/legs?precision=6&includeLayersIds=GaresPeage';
|
const polylineCoords = polylineLib.decode(encodedPolyline);
|
||||||
const body = JSON.stringify(encodedPolyline);
|
const ulysUrl = 'https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage';
|
||||||
const res = await axios.post(url, body, {
|
let finalPolyline = encodedPolyline;
|
||||||
|
|
||||||
|
// OPTION 1 : Mapbox Route Recreation
|
||||||
|
if (process.env.MAPBOX_API_KEY && polylineCoords.length > 2) {
|
||||||
|
logger.info('[Travel] MAPBOX_API_KEY is present. Recreating route with Mapbox for Ulys precision...');
|
||||||
|
try {
|
||||||
|
// Extraire jusqu'à 25 waypoints (limite Mapbox Directions API) équidistants
|
||||||
|
const maxWaypoints = 25;
|
||||||
|
const waypoints = [];
|
||||||
|
const step = Math.max(1, Math.floor(polylineCoords.length / (maxWaypoints - 1)));
|
||||||
|
for (let i = 0; i < polylineCoords.length; i += step) {
|
||||||
|
waypoints.push(polylineCoords[i]);
|
||||||
|
}
|
||||||
|
if (waypoints[waypoints.length - 1] !== polylineCoords[polylineCoords.length - 1]) {
|
||||||
|
waypoints[waypoints.length - 1] = polylineCoords[polylineCoords.length - 1]; // Ensure last point is exactly destination
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mapbox expects longitude,latitude
|
||||||
|
const coordinatesString = waypoints.map(p => `${p[1]},${p[0]}`).join(';');
|
||||||
|
const mapboxUrl = `https://api.mapbox.com/directions/v5/mapbox/driving/${coordinatesString}?geometries=polyline&overview=full&access_token=${process.env.MAPBOX_API_KEY}`;
|
||||||
|
|
||||||
|
const mapboxRes = await axios.get(mapboxUrl);
|
||||||
|
if (mapboxRes.data && mapboxRes.data.routes && mapboxRes.data.routes.length > 0) {
|
||||||
|
finalPolyline = mapboxRes.data.routes[0].geometry;
|
||||||
|
logger.info('[Travel] Mapbox route recreation successful.');
|
||||||
|
}
|
||||||
|
} catch (mapboxErr) {
|
||||||
|
logger.error('[Travel] Mapbox API error:', mapboxErr.response ? mapboxErr.response.data : mapboxErr.message);
|
||||||
|
// Fallback to Google Maps polyline if Mapbox fails
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Appeler Ulys /legs
|
||||||
|
const res = await axios.post(
|
||||||
|
ulysUrl,
|
||||||
|
JSON.stringify(finalPolyline),
|
||||||
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Content-Length': Buffer.byteLength(body).toString(),
|
'Host': 'api-ulys.azure-api.net'
|
||||||
'Host': 'api-ulys.azure-api.net',
|
|
||||||
},
|
},
|
||||||
timeout: 10000,
|
timeout: 10000,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.warn('[Travel] Ulys /legs failed:', e.message);
|
logger.warn('[Travel] Ulys /legs failed:', e.message);
|
||||||
|
|||||||
Reference in New Issue
Block a user