routeTask.solve() doesn't work

242
2
Jump to solution
07-11-2019 08:56 AM
ShakilChoudhury
New Contributor III

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
  };
}
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Shakil,

  Add an otherwise function to capture the error you are receiving or just look at your network traffic or browser web console to see whats going wrong.

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Shakil,

  Add an otherwise function to capture the error you are receiving or just look at your network traffic or browser web console to see whats going wrong.

0 Kudos
ShakilChoudhury
New Contributor III

Ah of course. Turns out I was adding geometrys to the routeparams instead of graphics.

0 Kudos