AnsweredAssumed Answered

Route Task Assistance

Question asked by dcoley on Oct 14, 2013
Latest reply on Oct 15, 2013 by dcoley
Hello-
If anyone can provide assistance with the following code, that would be great.  The code works, a route is defined using our internal NA, but does not draw a graphic route upon returning results.  I feel certain I have mis-called the route results, and/or am not passing in graphics correctly.  Here is the code.  It is culled from the existing sample, I have attempted to update syntax to 1.9

var mapMain, routeTask, routeParams, stopSymbol, routeSymbol, lastStop;

// @formatter:off
require([
  "esri/map",
  "esri/geometry/Extent",
  "esri/graphic",
  "esri/symbols/SimpleMarkerSymbol",
  "esri/symbols/SimpleLineSymbol",
  "esri/tasks/FeatureSet",
  "esri/tasks/RouteTask",
  "esri/tasks/RouteParameters",
  "esri/tasks/RouteResult",
  "esri/units",


  "dojo/parser",
  "dojo/ready",
  "dojo/_base/Color",

  "dijit/layout/BorderContainer",
  "dijit/layout/ContentPane"],
  function(
    Map, Extent, Graphic, SimpleMarkerSymbol, SimpleLineSymbol, FeatureSet, RouteTask, RouteParameters, RouteResult, Units,
    parser, ready, delcare, Color, BorderContainer, ContentPane
  ) {
  ready(function() {

mapMain = new Map("cpCenter", {
   basemap : "streets",
   extent : extentInitial,
   lods : myLods,
   showAttribution: false,
  });
 
  mapMain.on("click", addStop);
 
  routeTask = new RouteTask("https://ags2.scgov.net/arcgis/rest/services/ScOperational/RoutingService/NAServer/Route");
 
  routeParams = new RouteParameters();
        routeParams.attributeParameterValues = [{attributeName: "Oneway", parameterName: "Restriction Usage", value: "Prohibited"},
{attributeName: "RestrictedTurns", parameterName: "Restriction Usage", value: "Prohibited"}];
        routeParams.outSpatialReference = mapMain.SpatialReference;
        routeParams.returnRoutes = true;
        routeParams.returnStops = true;
        routeParams.returnDirections = true;
        routeParams.directionsLengthUnits = Units.MILES;
        routeParams.impedanceAttribute="Length";
        routeParams.stops = new FeatureSet();
       
        routeTask.on("solve-complete", showRoute);
        routeTask.on("error", errorHandler);

function addStop(evt) {
  stopSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CROSS, 20,
            new SimpleLineSymbol(
                SimpleLineSymbol.STYLE_SOLID,
                new Color([89, 95, 35]), 2
                ),
                new Color([130, 159, 83, 0.40])
                );
              
   var stop = new Graphic(evt.mapPoint, stopSymbol);
              mapMain.graphics.add(stop);
                routeParams.stops.features.push(stop);
  
   if (routeParams.stops.features.length >= 2) {
                   routeTask.solve(routeParams);
                   lastStop = routeParams.stops.features.splice(0, 1)[0];  //what does?
            }
   console.log(lastStop);
  }
function showRoute(solveResult) {
   routeSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new Color([254, 0, 0]), 3);
   mapMain.graphics.add(solveResult.routeResults[0].route.setSymbol(routeSymbol));
       }

function errorHandler(err) {
        alert("An error occured\n" + err.message + "\n" + err.details.join("\n"));

        routeParams.stops.features.splice(0, 0, lastStop);
        mapMain.graphics.remove(routeParams.stops.features.splice(1, 1)[0]);
        
       }
   });

});

Thanks
David

Outcomes