jacoblzj

Need help with routing codes!

Discussion created by jacoblzj on Mar 27, 2012
Latest reply on Mar 27, 2012 by ciava.at
These are the codes that me and my partner have done so far.. However we can't seem to solve the routes from Point A to Point B

<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title>Happy Family Portal</title>
    <script type="text/javascript" src="http://www.onemap.sg/API/JS?accessKEY=xkg8VRu6Ol+gMH+SUamkRIEB7fKzhwMvfMo/2U8UJcFhdvR4yN1GutmUIA3A6r3LDhot215OVVkZvNRzjl28TNUZgYFSswOi"></script>
    <script type="text/javascript">

        dojo.require("esri.map");  
        dojo.require("esri.tasks.query");
        dojo.require("esri.tasks.geometry");
        dojo.require("esri.toolbars.draw");
        dojo.require("esri.tasks.route");
//routing
       
        var divMain, routeTask, routeParams;
        var stopSymbol, routeSymbol, lastStop, barrierSymbol;
        var segmentSymbol, segmentGraphic;
        var OneMap = new GetOneMap('divMain', 'SM');
        var totalDistance = 0;
        var totalTime = 0;
        var f;
        var mapOnClick_addBarriers_connect, mapOnClick_addStops_connect;
        var dojoConnections = [];
        var barriersArray = [];

        }
       
        function initStop() {
  
            mapOnClick_addStops_connect = dojo.connect(map, "onClick", addStop);

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

            stopSymbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE).setSize(15);
            stopSymbol.outline.setWidth(4);
        }

        function solveRoute(routeParams) {

            routeTask.solve(routeParams);        
        }

        function clearRoutes() {

            for (var i = routes.length-1; i>=0; i--) {
                map.graphics.remove(routes.splice(i, 1)[0]);
            }
   
            routes = [];
        }

        //Clear barriers

        function clearBarriers() {
            removeEventHandlers();
            for (var i = routeParams.barriers.features.length - 1; i >= 0; i--) {
                map.graphics.remove(routeParams.barriers.features.splice(i, 1)[0]);
            }
        }


        function initBarrier() {

            removeEventHandlers();
            mapOnClick_addBarriers_connect = dojo.connect(map, "onClick", addBarrier);

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

            barrierSymbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CROSS).setSize(10);
            barrierSymbol.outline.setWidth(3).setColor(new dojo.Color([255, 0, 0]));
        }

        function addBarrier(evt) {
            var barrier = map.graphics.add(new esri.Graphic(evt.mapPoint, barrierSymbol));
            routeParams.barriers.features.push(barrier);
            //barriersArray.push(barrier);
            //routeTask.solve(routeParams);
        }

        function removeEventHandlers() {
            dojo.disconnect(mapOnClick_addBarriers_connect);
            dojo.disconnect(mapOnClick_addStops_connect);

        }

        //Adds the solved route to the map as a graphic
        function showRouteV(routeResults, barriers, messages) {
            clearRoutes();

            var distance;
            var time;
            var directions = routeResults[0].directions;
            var directionFeatures = directions.features;
            map.graphics.add(routeResults[0].route.setSymbol(routeSymbol));


            distance = formatDistance(directions.totalLength);
            totalDistance += distance;
            time = minsCal(directions.totalTime);
            totalTime += time;

             dojo.byId("divResults").innerHTML = "  Total distance: " + totalDistance + "<br />   Total time: " + totalTime;


            dojo.forEach(routeResults[0].directions.features, function (feature, i) {

                dirStrings.push("<li onclick='zoomToSegment(" + i + "); return false;' class=\"segment\"><a href=\"#\">" + feature.attributes.text + " (" + formatDistance(feature.attributes.length, "km") + ", " + formatTime(feature.attributes.time) + ")</a></li>");

            });

            dojo.forEach(routeResults, function (routeResult, i) {
                routes.push(map.graphics.add(routeResult.route.setSymbol(routeSymbols[routeResult.routeName])));
            });


            dojo.byId("divResults").innerHTML = dirStrings.join("");
            dojo.byId("divResult").innerHTML = "  Total distance: " + roundDistance(totalDistance, 2) + " Kilometres <br />   Total estimated time: " + roundTime(totalTime, 2) + " min(s)";
        }


        //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]);
        }
        function zoomToSegment(index) {

            var segment = directionFeatures.route[index];

            map.setExtent(segment.geometry.getExtent(), true);
            if (!segmentGraphic) {
                segmentGraphic = map.graphics.add(new esri.Graphic(segment.geometry, segmentSymbol));
            }
            else {
                segmentGraphic.setGeometry(segment.geometry);
            }

        }

        //Format the time as hours and minutes
        function formatTime(time) {
            var hr = Math.floor(time / 60), //Important to use math.floor with hours
            min = Math.round(time % 60);

            if (hr < 1 && min < 1) {
                return "";
            }
            else if (hr < 1) {
                return min + " minute(s)";
            }

            return hr + " hour(s) " + min + " minute(s)";

        }

        //Round the distance to the nearest hundredth of a unit
        function formatDistance(dist) {
            var d = Math.round(dist * 100) / 100;
            if (d === 0) {
                return ""
            }

            return d;
        }

        //Round the distance to 2 decimal places
        function roundDistance(dist, dec) {
            var result = Math.round(dist * Math.pow(10, dec)) / Math.pow(10, dec);
            return result;
        }

        //to store the time in mins
        function minsCal(time) {
            var min = time;
            return time;
        }

        //round the time to 2 decimal place
        function roundTime(time, dec) {
            var result = Math.round(time * Math.pow(10, dec)) / Math.pow(10, dec);
            return result;
        }


        //endroute

   
        function GetRouting(routeParams) {

            routeTask.solve(routeParams);
            alert("Please wait.........Route is being calculated.");
        }

        function showRoute(routeResults, barriers, messages) {
            clearRoutes();

            directions = routeResults[0].directions;
            directionFeatures = directions.features;
            //Add route to the map
            dojo.forEach(routeResults, function (routeResult, i) {
                routes.push(map.graphics.add(new esri.Graphic(directions.mergedGeometry, routeSymbol)))
            });



<body class="tundra">
    <form id="form1" runat="server">
    <table style="width: 1309px">
        <tr>
            <th colspan="3" class="style7" align="left">
      
       <div id="Header1" style="float: left">
       <asp:Image ID="Image1" runat="server" Height="101px"
                    ImageUrl="~/images/HF_logo2.gif" Width="321px" />
       </div>

    
            </th>
        </tr>
        <tr>
                                           
            <td style="vertical-align: top" class="style4" >
              
               
               
                <div id="SearchCategory">
               
               
                    <asp:Image ID="Image3" runat="server" Height="40px"
                        ImageUrl="~/images/Headers/dynamicrouting_header.gif" Width="170px" />
                    <a id="displayText" href="javascript:toggle();" style="vertical-align: middle">[+]</a>
                   
                    <div id="toggleText" style="display: none">
                   
                  
                   
                    <input type="button" onclick="initStop();" value="Add Stops"
                    style="width: 207px"/>
                    <input type="button" onclick="removeEventHandlers();" value="Stop"
                    style="width: 207px"/>
                    <input type="button" onclick="initBarrier();" value="Add Barrier"
                    style="width: 207px"/>
                    <input type="button" onclick="solveRoute();" value="Solve Route"
                    style="width: 207px"/>
                    <input type="button" onclick="clearBarriers();" value="Clear barriers"
                    style="width: 207px"/>
                    </div>

                    <br />
                </div>
</form>
</body>
</html>

Outcomes