Directions widget "getDirections()" method does not route

946
8
Jump to solution
03-21-2014 07:34 AM
JamesSampica
New Contributor
When my map loads I want to have the route automatically calculated and displayed without clicking the "Get Directions" button.

on https://developers.arcgis.com/javascript/jsapi/directions-amd.html#getdirections is lists the method getDirections() that says will automatically route and display the inputs.
directions = new Directions({                     map: map,                     stops: ["Knoxville ", "Chattanouga"], },"directionsDiv"); directions.startup(); directions.getDirections();


I load the inputs using "stops" parameter and call getDirections() but it does not display or route anything and instead gives me "Enter an origin and a destination." despite both inputs being correctly filled. If I click the button with the same inputs it will give me the correct route.

JSFiddle
http://jsfiddle.net/shoe788/XLSqF/
0 Kudos
1 Solution

Accepted Solutions
JonathanUihlein
Esri Regular Contributor
Hey James,

It looks like this is an unintended bug. It will be fixed in the next version.

In the meantime, here is a work-around using one of the internal ("private") functions.

                   var directions = new Directions({                     map: map                 }, "dir");          directions.on("load", function(){           directions._updateStops(["Knoxville ", "Chattanouga"]).then(function(stops){             console.log("stops object: ", stops);             directions.getDirections();           });         });


Note: For this work-around, don't set the 'stops' parameter in the Direction widget constructor.
We are using the deferred returned by _updateStops() to confirm that the stops have been properly loaded.

View solution in original post

0 Kudos
8 Replies
JonathanUihlein
Esri Regular Contributor
Hi and welcome to the forums!

Are you waiting for both the DOM and the map to load before calling getDirections()?
0 Kudos
JamesSampica
New Contributor
Hi and welcome to the forums!

Are you waiting for both the DOM and the map to load before calling getDirections()?


This could be the issue. If I attach `getDirections()` to a button and click it after the map loads, it works fine.

How can I make sure the map is loaded before calling getDirections()?
0 Kudos
JonathanUihlein
Esri Regular Contributor
You can use DOJO's AMD style event listener, on, and listen for the 'load' event that the map emits.

http://dojotoolkit.org/reference-guide/1.9/dojo/on.html#dojo-on

Blog:
http://dojotoolkit.org/documentation/tutorials/1.9/events/


          on(map, "load", function(){
            // the map has been loaded, do the following
            directions.getDirections();
          });


edit*

This should also work (same thing written differently).

          map.on("load", function(){
            // the map has been loaded, do the following
            directions.getDirections();
          });
0 Kudos
JamesSampica
New Contributor
You can use DOJO's AMD style event listener, on, and listen for the 'load' event that the map emits.

http://dojotoolkit.org/reference-guide/1.9/dojo/on.html#dojo-on

Blog:
http://dojotoolkit.org/documentation/tutorials/1.9/events/


          on(map, "load", function(){
            // the map has been loaded, do the following
            directions.getDirections();
          });


edit*

This should also work (same thing written differently).

          map.on("load", function(){
            // the map has been loaded, do the following
            directions.getDirections();
          });


The method never executes. I think the map is being loaded before the directions route is executed.

if I do this line

console.log(map.loaded);
directions.getDirections();


I get output "true" but directions does not route.
0 Kudos
JamesSampica
New Contributor
Here is a JS Fiddle documenting this

http://jsfiddle.net/shoe788/XLSqF/

You can see that when the map loads all it does is output "Enter an origin and a destination." instead of executing the route
0 Kudos
JonathanUihlein
Esri Regular Contributor
Hey James,  Thanks for that sample. I am also running into an issue getting this to work.   I'll find out more and post an update.
0 Kudos
JonathanUihlein
Esri Regular Contributor
Hey James,

It looks like this is an unintended bug. It will be fixed in the next version.

In the meantime, here is a work-around using one of the internal ("private") functions.

                   var directions = new Directions({                     map: map                 }, "dir");          directions.on("load", function(){           directions._updateStops(["Knoxville ", "Chattanouga"]).then(function(stops){             console.log("stops object: ", stops);             directions.getDirections();           });         });


Note: For this work-around, don't set the 'stops' parameter in the Direction widget constructor.
We are using the deferred returned by _updateStops() to confirm that the stops have been properly loaded.
0 Kudos
JamesSampica
New Contributor
Hey James,

It looks like this is an unintended bug. It will be fixed in the next version.

In the meantime, here is a work-around using one of the internal ("private") functions.

          
        var directions = new Directions({
                    map: map
                }, "dir");

        directions.on("load", function(){
          directions._updateStops(["Knoxville ", "Chattanouga"]).then(function(stops){
            console.log("stops object: ", stops);
            directions.getDirections();
          });
        });


Note: For this work-around, don't set the 'stops' parameter in the Direction widget constructor.
We are using the deferred returned by _updateStops() to confirm that the stops have been properly loaded.


Thanks for looking into this Jonathan,

I am seeing this workaround works as expected.

Thanks.
0 Kudos