Select to view content in your preferred language

Get driving directions sample customization

4763
15
05-17-2011 11:44 PM
AlexeyTereshenkov
Deactivated User
Hi all,

My aim is to create a web application where a user could click on the map or type in the address end and start points for the routing; the route will be calculated and shown on the map; the directions text will be shown on the html page to the user. This application I need exclusively for testing of the routing capabilities available via the ArcGIS Web API.

It seemed that the easiest way would be to customize the sample Get driving directions (http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm#jssa...) to use European geocoding and routing services.

I have exchanged the locator and the routing services strings with the European services:

locator = new esri.tasks.Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Address_EU/GeocodeServer");


and

routeTask = new esri.tasks.RouteTask("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_EU/NAServer/Route");


However, when trying to get the directions, I get the error message from the browser:

An error occured
Unable to complete  operation.
Required field '<i>Country</i>' must be specified


I understand that probably this coming from the code where the parsing of the input geocoding strings happen. I.e.

//Parse and geocode "from" address
        var fromArr = dojo.byId("fromTxf").value.split(","),
            fromAddress = { Address:fromArr[0], City:fromArr[1], State:fromArr[2], Zip:fromArr[3] };
        locator.addressToLocations(fromAddress, ["Loc_name"], function(addressCandidates) { configureRoute(addressCandidates, "from"); });

        //Parse and geocode "to" address
        var toArr = dojo.byId("toTxf").value.split(","),
            toAddress = { Address:toArr[0], City:toArr[1], State:toArr[2], Zip:toArr[3] };
        locator.addressToLocations(toAddress, ["Loc_name"], function(addressCandidates) { configureRoute(addressCandidates, "to"); });
      }


However, I am not sure how one is supposed to edit this piece of code (or maybe others, too!) in order to reflect the European geocoding parameters (I've looked a bit at http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Address_EU/GeocodeServer).

I would appreciate if anyone could give me some tips what shall I look at. Maybe it is easier to customize the Find Route sample (http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm#jssa...) by implementing the routing directions there, but I am not that familiar with the JavaScript API.

Thank you.
0 Kudos
15 Replies
HemingZhu
Frequent Contributor
tereshenkov;103217 wrote:
Heming,


That is very nice of you. The code is the exact copy of the sample page one can get from the Resource Center apart from the REST URL for the geocoding and routing services (now European) plus those address variable parameters you have helped me to declare.

The address you defined has to match with the locator's Address Fields listed on the locator service directory. I modified several lines in ESRI's driving directions to use the EU locator and Router you mention. Please pay attention to how to define the address object in the code:

Code attached
0 Kudos
AlexeyTereshenkov
Deactivated User
Heming,

Thank you very much for your help, I appreciate this. I have got the  "from" point geocoded now and zoomed to on the map, but it seems that driving directions do not work properly yet.

Please see the code page with the address modified in the input text boxes (I have not changed anything apart from the input addresses):

<input type="text" id="fromTxf" value="1 Kungsgatan, Stockholm, 11143, Sweden" size="30" />
    <input type="text" id="toTxf" value="10 Parkvagen, Gavle, 80267, Sweden" size="30" />


The application treats the input start point to be the end point as well, so I do not get any route calculated, since the start and the end point coincide.

I have looked into the syntax and did not find any typo or alike, so my guess is that it has something to do with the syntax. I know that it is very hard to debug the JavaScript code (you get Intellisense in Aptana and that is basically it), but I would be beholden to you if you could take a look at the code when you get a chance.
0 Kudos
HemingZhu
Frequent Contributor
Heming,

Thank you very much for your help, I appreciate this. I have got the  "from" point geocoded now and zoomed to on the map, but it seems that driving directions do not work properly yet.

Please see the code page with the address modified in the input text boxes (I have not changed anything apart from the input addresses):

<input type="text" id="fromTxf" value="1 Kungsgatan, Stockholm, 11143, Sweden" size="30" />
    <input type="text" id="toTxf" value="10 Parkvagen, Gavle, 80267, Sweden" size="30" />


The application treats the input start point to be the end point as well, so I do not get any route calculated, since the start and the end point coincide.

I have looked into the syntax and did not find any typo or alike, so my guess is that it has something to do with the syntax. I know that it is very hard to debug the JavaScript code (you get Intellisense in Aptana and that is basically it), but I would be beholden to you if you could take a look at the code when you get a chance.


I think there is a bug the ESRI has to look into. It all has something to do with the Json expression of the address object (looked at the this thread: http://forums.arcgis.com/threads/23457-Geocoding-Service-Not-returning-Candidates-to-quot-Applicatio...). So i changed the address Json object to Object. For anyone who has struggled with Json expression of the object (subset of javascript object), use javascript object. I have tried, it is more flexible(no order to follow!) and  never failed me.  Attached is the code.
0 Kudos
AlexeyTereshenkov
Deactivated User
Hi Heming,

Thank you very much for the help! Now it works as required. The only thing that is left is to figure out how the routing can work based on the address and not only on the postcode. I guess you have not noticed that if you choose to route between addresses with the same postcode, the route is not calculated.

You might try to enter as an input and an output points the addresses below:
20 Parkvagen, Gavle, 80267, Sweden
and
1 Parkvagen, Gavle, 80267, Sweden

As you'd see in the Directions window, the route starts at 80267 and ends at 80267. That is, the solver does not parse the addresses properly.

If you could take a look at this problem, I would really appreciate this. But, again, this is not a big deal, that is still good enough for testing purposes.

Thank you again for the help, Heming, much appreciated.
0 Kudos
HemingZhu
Frequent Contributor
Hi Heming,

Thank you very much for the help! Now it works as required. The only thing that is left is to figure out how the routing can work based on the address and not only on the postcode. I guess you have not noticed that if you choose to route between addresses with the same postcode, the route is not calculated.

You might try to enter as an input and an output points the addresses below:
20 Parkvagen, Gavle, 80267, Sweden
and
1 Parkvagen, Gavle, 80267, Sweden

As you'd see in the Directions window, the route starts at 80267 and ends at 80267. That is, the solver does not parse the addresses properly.

If you could take a look at this problem, I would really appreciate this. But, again, this is not a big deal, that is still good enough for testing purposes.

Thank you again for the help, Heming, much appreciated.


It's rather the locator then the solver (you can debug it). One work around is replace the begin and end directions description with from and end address (The begin and end stop point are accurate address points. i recalled this is what i did in one of my project).
0 Kudos
AlanaD_Andrea
Emerging Contributor
I am wondering if there is a way to add direction map tools to a map I already have created. I have an asp.net mvc 4 application with a map that I need to add a direction widget too. I have been trying to follow your forum for help and also this documentation but I am not having any luck. https://developers.arcgis.com/en/javascript/jssamples/widget_directions_basic.html
I am quite new to this coding but I am thinking I just need to modify or replace some of the code.

Any advice or support would be greatly appreciated.

thank you in advance.
0 Kudos