Get directions route info details of Directions Widget

755
4
Jump to solution
10-28-2021 05:03 AM
benchikh
New Contributor III

Hi,

Im working with arcgis api 3.38 Directions Widget, I have declared the directions widget as below:

var directions = new Directions({
map: map,
showClearButton: true,
showSegmentPopup: false,
directionsLengthUnits: "esriMiles",
mapClickActive: true,
routeTaskUrl: "https://xxxxxxxx/arcgis/rest/services/CALCUL_ITINERAIRE/NAServer/Route",
searchOptions: options
}, "dir");
directions.startup();

The question is, there is a way to recuperate the Directions route info Details (Start Point, Time,... Finish Point, time) as I want to send them to a web service URL

Many thanks for your help

 

 

 

0 Kudos
1 Solution

Accepted Solutions
benchikh
New Contributor III

I have solved the  issue as below, Many thanks for your support

directionsW.on("directions-finish", changeHandler);

function changeHandler(data) {
console.log("done", data.result);
if (data.result.routeResults.length > 0) {
const features = data.result.routeResults[0].stops;
features.forEach(function (result, i) {
console.log(result.attributes.Name );
});
}
}

View solution in original post

4 Replies
Noah-Sager
Esri Regular Contributor

You should be able to listen to the `directions-finish` event, which returns a RouteResult that you can then handle as you wish.

https://developers.arcgis.com/javascript/3/jsapi/directions-amd.html#event-directions-finish

 

benchikh
New Contributor III

Many thanks for your reply, could please describe it with a sample of code as Im a beginner and dont have enough experience to handle it please .

0 Kudos
benchikh
New Contributor III

I have solved the  issue as below, Many thanks for your support

directionsW.on("directions-finish", changeHandler);

function changeHandler(data) {
console.log("done", data.result);
if (data.result.routeResults.length > 0) {
const features = data.result.routeResults[0].stops;
features.forEach(function (result, i) {
console.log(result.attributes.Name );
});
}
}

benchikh
New Contributor III

Hi I have tried to sort it out, and have created somthing like below

directions.on("directions-finish", changeHandler);

function changeHandler(data) {
if (data.routeResults.length > 0) {
const features = data.routeResults[0].directions.features;
features.forEach(function (result, i) {
console.log(result.attributes.text);
});

But I have got a message error :

TypeError: Cannot read properties of undefined (reading 'length')

0 Kudos