xhr error when more than 5 stops used in a RouteTask

2174
7
Jump to solution
08-19-2013 05:05 AM
MichaelSpivey
New Contributor II
When I run the following code it errors saying "Unable to get value of the property 'xhr': object is null or undefined". If I comment out the stop6 variable and only include 5 stops in the request then it works fine and no errors. This is using the sample server, because I'm currently in the process of evaluating this service. Online I've been able to find where there is a limit of 10 stops for the sample server, but not 5 stops.

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title>Simple Routing</title>

    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>

    <script type="text/javascript">
        dojo.require("esri.map");
        dojo.require("esri.tasks.route");

        var map, routeTask, routeParams;
        var stopSymbol, routeSymbol, lastStop;
        var stopCount = 0;
        var stop1, stop2, stop3, stop4, stop5;

        function init() {
            map = new esri.Map("map", {
                extent: new esri.geometry.Extent({ xmin: -117.22111701965332, ymin: 34.039506912231445, xmax: -117.16961860656738, ymax: 34.07383918762207, spatialReference: { wkid: 4326} })
            });

            map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"));
            dojo.connect(map, "onClick", addStop);

            routeTask = new esri.tasks.RouteTask("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route");
            routeParams = new esri.tasks.RouteParameters();
           
            //All of these param options need to be set to get back the results needed.
            routeParams.useTimeWindows = true;
            routeParams.findBestSequence = true;
            routeParams.preserveFirstStop = true;
            routeParams.preserveLastStop = false;
            routeParams.returnDirections = true;
            routeParams.returnStops = true;

            routeParams.stops = new esri.tasks.FeatureSet();

            dojo.connect(routeTask, "onSolveComplete", showRoute);
            dojo.connect(routeTask, "onError", errorHandler);

            stopSymbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CROSS).setSize(15);
            stopSymbol.outline.setWidth(4);
            routeSymbol = new esri.symbol.SimpleLineSymbol().setColor(new dojo.Color([0, 0, 255, 0.5])).setWidth(5);

            //Add some points programmatically. This creates a route with time windows.
            setTimeout(function () {
                stop1 = addStop(createPoint([-117.18249320983887, 34.057273864746094], new esri.SpatialReference({ wkid: 4326 }), "8/16/2013 8:00 AM", "8/16/2013 8:15 AM", "stop1"));
                stop2 = addStop(createPoint([-117.18349320983887, 35.037273864746094], new esri.SpatialReference({ wkid: 4326 }), "8/16/2013 8:00 AM", "8/16/2013 10:30 AM", "stop2"));
                stop3 = addStop(createPoint([-117.18359320983887, 34.037393864746094], new esri.SpatialReference({ wkid: 4326 }), "8/16/2013 10:55 AM", "8/16/2013 1:00 PM", "stop3"));
                stop4 = addStop(createPoint([-118.18359320983887, 35.037393864746094], new esri.SpatialReference({ wkid: 4326 }), "8/16/2013 3:00 PM", "8/16/2013 5:30 PM", "stop4"));
                stop5 = addStop(createPoint([-118.18359320983887, 36.037393864746094], new esri.SpatialReference({ wkid: 4326 }), "8/16/2013 1:55 PM", "8/16/2013 3:00 PM", "stop5"));
                stop6 = addStop(createPoint([-117.12249320983887, 34.057273864746094], new esri.SpatialReference({ wkid: 4326 }), "8/16/2013 4:00 PM", "8/16/2013 8:15 PM", "stop6"));

                //Solve the route
                if (routeParams.stops.features.length >= 2) {
                    routeTask.solve(routeParams);
                    //lastStop = routeParams.stops.features.splice(0, 1)[0];
                }
            }, 3000);
        }

        //Create an object to store the point and time window data
        function createPoint(latLng, spatialReference, timeWindowStart, timeWindowEnd, name) {
            var evt = new Object();
            evt.mapPoint = new esri.geometry.Point(latLng, spatialReference);
            evt.timeWindowStart = timeWindowStart;
            evt.timeWindowEnd = timeWindowEnd;
            evt.name = name;
            return evt;
        }

        dojo.addOnLoad(init);

        //Adds a graphic when the user clicks the map.
        function addStop(evt) {
            var stop = map.graphics.add(new esri.Graphic(evt.mapPoint, stopSymbol));
            stop.attributes = new Object();
           
            //If a time window is set then copy it over to the stop object
            if (evt.timeWindowStart != null && evt.timeWindowEnd != null) {
                stop.attributes.TimeWindowStart = evt.timeWindowStart;
                stop.attributes.TimeWindowEnd = evt.timeWindowEnd;
                stop.attributes.Name = evt.name;
            }

            //Add the stop to the routeParams array
            routeParams.stops.features.push(stop);

            return stop;

        }

        //Adds the solved route to the map as a graphic
        function showRoute(routeResults, barriers) {
            map.graphics.add(routeResults[0].route.setSymbol(routeSymbol));
           
        }

        //Displays any error returned by the Route Task
        function errorHandler(err) {
            alert("An error occured\n" + err.message + "\n" + err.details.join("\n"));

            routeParams.stops.features.splice(0, 0, lastStop);
            map.graphics.remove(routeParams.stops.features.splice(1, 1)[0]);
        }
    </script>

  </head>
  <body class="tundra">
    <div id="map" style="width:600px; height:400px; border:1px solid #000;"></div>
    <p style="display:none">Click on the map to add stops to the route. The route from the last stop to the newly added stop is calculated. If a stop is not reachable, it is removed and the last valid point is set as the starting point.</p>
  </body>
</html>

[/HTML]
0 Kudos
1 Solution

Accepted Solutions
JohnGravois
Frequent Contributor
actually, i changed my mind.  after i updated your app to use the current version of our API, rather than version 1.6, the console notified me that a proxy was necessary, and after adding it i got a valid response from the network analyst service.

<!--script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script--> <script type="text/javascript" src="http://js.arcgis.com/3.6/"></script> <link rel="stylesheet" href="http://js.arcgis.com/3.6/js/esri/css/esri.css">

View solution in original post

0 Kudos
7 Replies
JohnGravois
Frequent Contributor
are you referencing a valid proxy in your application?  i have a hunch you're exceeding the limit of what can be sent without POSTing.
0 Kudos
MichaelSpivey
New Contributor II
Thanks for the reply. I have added the proxy like you suggested and verified with a breakpoint that the esri JavaScript is going through the proxy, but the JavaScript error is still occuring when I add the 6th stop. Any other ideas?
0 Kudos
MichaelSpivey
New Contributor II
Also, it was not hitting the proxy until I added esri.config.defaults.io.alwaysUseProxy = true; so it does not appear that the URL is going over that ~2000 character threshold.
0 Kudos
JohnGravois
Frequent Contributor
sounds like my speculation about the proxy was off base then.

what happens when you truncate the stop coordinates to 6 or 7 decimal places? you are currently sending quite a few significant digits..
0 Kudos
JohnGravois
Frequent Contributor
actually, i changed my mind.  after i updated your app to use the current version of our API, rather than version 1.6, the console notified me that a proxy was necessary, and after adding it i got a valid response from the network analyst service.

<!--script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script--> <script type="text/javascript" src="http://js.arcgis.com/3.6/"></script> <link rel="stylesheet" href="http://js.arcgis.com/3.6/js/esri/css/esri.css">
0 Kudos
MichaelSpivey
New Contributor II
Yes, the new script reference seems to have fixed it. Thanks
0 Kudos
JohnGravois
Frequent Contributor
glad to hear it.  please consider marking this thread as 'answered'.
0 Kudos