AnsweredAssumed Answered

routeTask.solve() doesn't work

Question asked by shakiltmbc on Jul 11, 2019
Latest reply on Jul 12, 2019 by shakiltmbc

I'm writing a custom directions widget because the default one doesn't have the functionality I need.

 

Loading in the from point and the to point work fine.

 

But on Line 60 my call to routeTask.solve() doesn't do anything? It doesn't even make a network request, which I've monitored through Chrome dev tools. Any ideas on what's going wrong? I've used routeTask.solve() successfully before. The only difference between the successful times and this, that I can see, is that this on a different file and is imported through dojo require.

 

function CustomDirections(options){
  var fromLoc = null;
  var toLoc = null;

  var view = options.view;
  var routeLayer = options.routeLayer;
  var routeTask = options.routeTask;
  var routeParams = options.routeParams;
  var routeSymbol = options.routeSymbol;
  var search = options.search;
  search.autoSelect = false;

  var directionsNode = document.createElement("div");
  directionsNode.className = "custom-directions";

  var searchNode = document.createElement("div");
  search.container = searchNode;
  directionsNode.appendChild(searchNode);

  var toLocation = document.createElement("p");
  toLocation.innerHTML = "Test";
  directionsNode.appendChild(toLocation);

  var routeWrapper = document.createElement("div");
  directionsNode.appendChild(routeWrapper);

  search.on("search-complete", function(event){
   setFromLocation(event);
   event.stopPropagation();
  });

  search.on("select-result", function(event){
   setFromLocation(event);
   event.stopPropagation();
  });

  function setFromLocation(event){
    console.log("setFromLocation => ", event.results[0].results[0].feature.geometry);
    fromLoc = event.results[0].results[0].feature.geometry;
    if(fromLoc !== null && toLoc !== null){
      renderRoute();
    }
  }

  function setToLocation(loc){
    console.log("setToLocation => ", loc);
    toLoc = loc;
    toLocation.innerHTML = loc.x.toFixed(0) + ", " + loc.y.toFixed(0);
    if(fromLoc !== null && toLoc !== null){
      renderRoute();
    }
  }

  function renderRoute(){
    routeLayer.removeAll();
    routeParams.stops.features = [];
    routeParams.stops.features.push(fromLoc);
    routeParams.stops.features.push(toLoc);

    routeTask.solve(routeParams).then(function(data){
      console.log(data);
    });
  }

  return {
    domNode: directionsNode,
    setToLocation: setToLocation
  };
}

Outcomes