AnsweredAssumed Answered

Directions widget - load route with waypoints

Question asked by andrewjwhittaker on Sep 13, 2016
Latest reply on Sep 15, 2016 by andrewjwhittaker

In the JS API 3.17, I am using the Directions widget for routing.  After getting directions, I am saving the stops to a SQL Server database in JSON format as returned in the directions.  I can load the first two stops to the directions widget by calling updateStop(...).  If I've got more than 2 stops, then calling addStop(...) will add another stop in the widget.  What I cannot figure out is how to load a start and end point (two stops in the directions widget) AND a waypoint, then allow the directions widget to recalculate directions via a call to getDirections().  The waypoint is not a stop-over point.  It's just the location to pass through.  I found that the feature property in each stop has an attribute for "isWaypoint".  I have tried checking each stop and if isWaypoint==true, then do not add the stop to the directions widget.  However, the call to network analyst Route/solve should still have the waypoint(s) so the route is calculated through those points.  I'm really just trying to replicate a route with waypoints programmatically.  Below is some code I've been playing with to load the stops from the database.  routeData is my own Object with properties that I fill from the database.  The StopsJson property has the JSON for each exactly as found in the directions widget.  Anyone done this or have any advice?  If I have not been clear, please comment below and I can elaborate.

 

loadStops: function (routeData) {
   if (routeData != null &&
   routeData.Stops != null &&
   routeData.Stops.length > 0) {
   var stops = JSON.parse(routeData.StopsJson);
   if (stops == null) {
      return;
   }
   var numStops = stops.length;
   var comments = routeData.Comments.join(". ");
   var numWaypoints = 0;
   var insertionIndex = 0;
   // NOTE: do we call Route?solve directly?
   //this.map.directions.stops = stops;
   for (var index = 0; index < numStops; index++) {
      var stop = stops[index];
      var point = new Point(stop.feature.geometry);
      if (stop.feature.attributes.isWaypoint) { //(stop.name != "DWWP") {
         // This is a waypoint      

         //insertionIndex = index;
         numWaypoints++;
         // The following line adds the stop (as waypoint) but  DOESN'T WORK with directions widget

         //this.map.directions.stops.splice(insertionIndex, 0, stop);
      }
      else {
         insertionIndex = index - numWaypoints;
         if (insertionIndex < 2) {
            this.map.directions.updateStop(point, insertionIndex);
         }
         else {
            this.map.directions.addStop(point, insertionIndex);
         }
      }
}


}
},

Outcomes