I want to solve the issue of the Vehicle Routing Problem (VRP) using ComputeRoutes google API (API), as it is more clear and supports nodejs.
I have already create the function with ComputeRoutes, the issue is that I can only create the best route for one vehicle, but what if I need multiple vehicles to complete the destinations?
It is possible?
Part of the Code:
const data = {
origin: locationOrigin,
destination: locationOrigin,
intermediates: [] as WayPoint[],
travelMode: "DRIVE",
routingPreference: "TRAFFIC_AWARE",
computeAlternativeRoutes: false,
optimizeWaypointOrder: true,
};
way = {} as Way;
orders.forEach((order: any) => {
const waypoint = {
placeId: order.order.address.placeId,
};
intermediates.push(waypoint);
});
data.intermediates = intermediates;
console.log("data ", data);
await doRequest(data, "routes")
.then((data: any) => {
console.log("yeei ", data);
console.log(
"optimizedIntermediateWaypointIndex ",
data.routes.optimizedIntermediateWaypointIndex
);
way = createWaysWithWaypoints(
data.routes[0],
orders,
riders[0],
data.routes[0].optimizedIntermediateWaypointIndex,
wayscount
);
ways.push(way);
//wayscount ++;
console.log("ways ", ways);
})
.catch((error: any) => {
errorMessage = error;
// Log the error message and code
console.log(`Error message: ${error.message}`);
console.log(`Error code: ${error.code}`);
// Log the response status and data if available
if (error.response) {
console.log(`Response status: ${error.response.status}`);
console.log(
`Response data: ${JSON.stringify(error.response.data)}`
);
}
// Log the request method and path if available
if (error.request) {
console.log(`Request method: ${error.request.method}`);
console.log(`Request path: ${error.request.path}`);
}
return {
statusCode: 400,
headers: {
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
},
body: JSON.stringify(error.message),
};
});
doRequest function:
function doRequest(data: any, opt: string) {
let result = "";
return new Promise(function (resolve, reject) {
var options = {
host: "routes.googleapis.com",
path: "/directions/v2:computeRoutes",
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Goog-Api-Key": "xyz",
"X-Goog-FieldMask":
"routes.optimizedIntermediateWaypointIndex,routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline,routes.routeLabels,routes.legs.distanceMeters,routes.legs.duration",
},
};
let post_req = http.request(options, function (res: any) {
//res.setEncoding('utf8');
let body = "";
res.on("data", function (chunk: any) {
body += chunk;
console.log("Response: ", body);
});
res.on("end", () => {
console.log("body ", JSON.parse(body));
resolve(JSON.parse(body));
});
res.on("error", function (e: any) {
console.log("Got error: " + e.message);
});
});
post_req.write(JSON.stringify(data));
post_req.end();
});
}
New contributor
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.