Select to view content in your preferred language

Distinguish route geometries between stops

08-15-2017 08:39 AM
Esri Contributor

Hello all, 

I'd like to display a specific part of a routing result in a different color than the others. I thought this would be easy, but it seems that it's not.

I'm using the Directions Widget of the JS API, and the standard config allows me to define a symbol for the whole route or for the selected segment (which is a direction segment, not the whole part from one stop to the next). So I could look for the "esriDMTDepart" direction where my part starts, decode all the compressedGeometries until the next "esriDMTStop" and draw them manually on the map over the route that is being drawn anyway. This is a bit ugly, so I'm going one level deeper to the NAServer REST API.

The routing service is able to give back a Polyline route geometry (return routes = true), but as far as I can see, it is always one line for the whole route. It won't mark where the stops are or separate the route geometry into parts between the stops. Scott Sandusky, Matt Crowder: To my opinion, this is a missed chance, because the Polyline JSON object is an array of several line objects.

There is no way to quickly tell where the stops are located on the line that comes back, because the coordinates of polyline points and stop point don't match exactly. Manual search shows that they may match up to the 5th decimal digit or so, but not to the end.

I could try to develop an approximation method to determine where the stops are located on the route line, but I'm not sure if this would produce reliable results.. or send an extra HTTP call for the part of the route that I want to draw differently, but that's of course costly and redundant.

Am I missing something? Does anyone have a good idea what to do?


2 Replies
Esri Frequent Contributor

If I understand the question correctly you want the individual route result segments? If so you can get them from the route results. Here's an example: 

        directions.on("directions-finish", function(routeResults){
          var dir =  routeResults.result.routeResults[0].directions;
            console.log("Segment", segment);
0 Kudos
Esri Contributor


thanks for your reply. Actually no: I'm not trying to color single segments, but the whole part of a route between 2 stops.

Iterating through the segments (and looking for the "esriDMTDepart" direction where my part starts and "esriDMTStop" where it ends) is what I was talking about in the first paragraph of my description above. But then I still have to decode the compressedGeometries, combine all segments from one stop to the next and draw the line (in a different color) on top the graphic that is being drawn by the Directions Widget anyway. This should be invisible to the end user, so it is possible to do what I want with this approach, but from a development point of view, it's a bit ugly.

My thought was to use the uncompressed route geometry and draw the whole thing myself, but then I realized that only one line comes back for the whole route, even though the returned JSON object is a Polyline. So we get an array of lines that always contains only one line. I'm suggesting that this should be separated into sections between stops (or the segments as uncompressed geometry) by the naserver‌.

Of course it would only be a performance win, if I could switch off route drawing on the directionswidget. Btw, I'm using the Directions widget without displaying the directions on the screen. Afaik there's no switch to disable directions on the Directions widget, so I'm hiding them via CSS. I'm wondering if I'm the only one who just needs a good UI for route calculation and no directions, but I guess that it's not such an uncommon use case. I think, flags to turn off route drawing and directions would be a nice enhancement‌ to the Directions widget. What do you think? (Rene Rubalcava‌?)

Anyway, I've handed in a idea and another idea.

Cheers, Nik

0 Kudos