Dispatch Optimized Routes to Geotab

To dispatch the generated routes to the MyGeoTab Web Application, follow these detailed steps. This process involves constructing a POST request from the routes generated by NextBillion.ai’s Route Optimization API and sending it to Geotab.

Step 1: Extract Relevant Data from NextBillion.ai Result

First, retrieve the optimization result data, including the routes, from the NextBillion.ai API response.

1
const nbaiResult = nbaiOptimizationStatus.data.result;
2
const routes = nbaiResult.routes;

Step 2: Define Helper Function to Convert Distance to Miles

Create a helper function to convert distances from meters to miles.

1
const convertToMiles = (distance) => distance * 0.00062137;

Step 3: Get Zone IDs from Dropdowns

Retrieve the zone IDs for the start and end locations from dropdown elements.

1
const startZoneId = select1.value;
2
const endZoneId = select2.value;

Step 4: Construct the POST Request Body

Build the request body for the POST request to dispatch routes to Geotab.

1
const postRequestBody = {
2
method: "ADD",
3
params: {
4
typeName: "Route",
5
entity: {
6
comment: "GeoTab_Nb_Integration",
7
name: routes[0].description,
8
routePlanItemCollection: routes[0].steps.map((step, index) => ({
9
activeFrom: dtShiftStart2.value,
10
activeTo: dtShiftEnd2.value,
11
dateTime: dtShiftStart2.value,
12
comment: "",
13
expectedDistanceToArrival: convertToMiles(step.distance),
14
expectedStopDuration: new Date(step.service * 1000).toISOString().substr(11, 8),
15
expectedTripDurationToArrival: new Date(step.duration * 1000).toISOString().substr(11, 8),
16
sequence: index,
17
zone: {
18
id: (index === 0) ? startZoneId : (index === routes[0].steps.length - 1) ? endZoneId : step.description
19
}
20
})),
21
routeType: "Basic",
22
startTime: dtShiftStart2.value,
23
endTime: dtShiftEnd2.value
24
},
25
credentials: {
26
"database": "geotab_database",
27
"sessionId": "session_id",
28
"userName": "user_name"
29
}
30
}
31
};

Step 5: Convert Request Body to JSON String

Convert the constructed request body to a JSON string for the POST request.

1
const requestBodyJson = JSON.stringify(postRequestBody);

Step 6: Print and Store the Request Body

Print the request body to the console and store it in local storage for further use.

1
console.log("Post Request Body:", requestBodyJson);
2
tmpRouteObject.setValue(postRequestBody);
3
4
// Store the request body in local storage
5
localStorage.setValue('tmpRouteGeotab', postRequestBody);

Step 7: Trigger the POST Request to Geotab

send_to_geotab.trigger is a function that sends the POST request, use it to dispatch the route to Geotab. Handle success and failure responses appropriately.

1
send_to_geotab.trigger({
2
onSuccess: function (data) {
3
console.log("Routes processed successfully:", data);
4
},
5
onFailure: function (error) {
6
console.error("Error processing routes:", error);
7
}
8
});

Step 8: Send POST Dispatch Request

Here’s an example of how the POST request will look using curl:

1
curl --location 'https://my.geotab.com/apiv1/dispatches' \
2
--header 'Content-Type: application/json' \
3
--data '<request body here>'

API Response

1
{
2
"result": "b9",
3
"jsonrpc": "2.0"
4
}

By following these steps, you can successfully dispatch the generated routes to the MyGeoTab Web Application.

The following images showcases the routes dispatched to your MyGeotab dashboard for each driver.

Visualize dispatch optimized routes in MyGeotab web application dashboard

Integrating NextBillion.ai’s Route Optimization API with the GeoTab Web streamlines the process of generating and dispatching optimized routes. This integration enhances operational efficiency by leveraging advanced route optimization capabilities, thus ensuring that drivers follow the most efficient paths for their deliveries.

© 2024 NextBillion.ai all rights reserved.