meyvn77

Closest Facility Example error

Discussion created by meyvn77 on Sep 21, 2012
Latest reply on Oct 5, 2012 by JGravois-esristaff
I have altered this closest Facility example to work with my own feature layer as the facilities but it does not solve.
I get this error when it enters
closestFacilityTask.solve(params, function (solveResult)
        //ERROR HERE... Fire BUG
            //Error: Unable to complete  operation.
            //http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.1
            //Line 34

When I check the params in fire bug it shows proper facilities (46) and the mouse click graphic as the incident.
Not sure why it would not solve this? Any help would be greatly appreciated...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=9" /> 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/> 
    <title></title>
     <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.1/js/esri/dijit/css/Popup.css">   
     <link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.1/js/dgrid/css/skins/tundra.css">
     <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.8/js/dojo/dojox/layout/resources/ExpandoPane.css"/>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.1/js/dojo/dijit/themes/claro/claro.css">
      <link href="Style.css" rel="stylesheet" type="text/css" />
       <style type="text/css">
     @import "http://serverapi.arcgisonline.com/jsapi/arcgis/3.1/js/dojo/dojox/layout/resources/FloatingPane.css";
  </style>
  <style type="text/css"> 
    body,html,#main{margin:0;padding:0;height:100%;width:100%;}
    .panel {
    -moz-border-radius: 6px;-webkit-border-radius: 6px;-moz-box-shadow: 0px 6px 3px -3px #888;-webkit-box-shadow: 0px 6px 3px -3px #888;
    box-shadow: 0px 6px 3px -3px #888;border: 2px solid #86942A;margin: 5px 5px 5px 5px;
  }
  </style> 
  <script type="text/javascript"> var djConfig = { parseOnLoad: true };</script> 
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.1"></script>
  <script type="text/javascript">
      dojo.require("esri.map");dojo.require("esri.tasks.closestfacility");dojo.require("dijit.dijit");dojo.require("dijit.form.ComboBox");
      dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("esri.layers.graphics");
      dojo.require("esri.layers.FeatureLayer"); dojo.require("esri.IdentityManager");

      var map, incidentsGraphicsLayer, routeGraphicLayer, featureLayerYard;
      var closestFacilityTask, params;
      var resizeTimer;

      function init() {
          var startExtent = new esri.geometry.Extent(-124.231, 23.9, -60.56, 52.19, new esri.SpatialReference({ wkid: 4326 }));
          map = new esri.Map("map", { extent: esri.geometry.geographicToWebMercator(startExtent) });

          dojo.connect(map, "onLoad", function () {
              //resize the map when the browser resizes
              dojo.connect(dijit.byId('map'), 'resize', map, map.resize);
          });

          dojo.connect(map, "onClick", mapClickHandler);

          var baseMapLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
          map.addLayer(baseMapLayer);

          //Yards
          var featureLayerYard = new esri.layers.FeatureLayer("http://services.arcgis.com/s94UJcOoEa23NHUs/arcgis/rest/services/Rigs_by_Yards/FeatureServer/0", {
              mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,
              id: "featureLayerYard"
          });
          map.addLayer(featureLayerYard);

          params = new esri.tasks.ClosestFacilityParameters();
          params.defaultCutoff = 50.0;
          params.returnIncidents = false;
          params.returnRoutes = true;
          params.returnDirections = true;

          dojo.connect(map, "onLoad", function (map) {

              var incidentPointSymbol = new esri.symbol.SimpleMarkerSymbol(
                esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE,16,
                new esri.symbol.SimpleLineSymbol( esri.symbol.SimpleLineSymbol.STYLE_SOLID,new dojo.Color([89, 95, 35]), 2),new dojo.Color([130, 159, 83, 0.40]));

              incidentsGraphicsLayer = new esri.layers.GraphicsLayer();

              var incidentsRenderer = new esri.renderer.SimpleRenderer(incidentPointSymbol);
              incidentsGraphicsLayer.setRenderer(incidentsRenderer);
              map.addLayer(incidentsGraphicsLayer);

              routeGraphicLayer = new esri.layers.GraphicsLayer();

              var routePolylineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,new dojo.Color([89, 95, 35]),4.0);
              var routeRenderer = new esri.renderer.SimpleRenderer(routePolylineSymbol);
              routeGraphicLayer.setRenderer(routeRenderer);

              map.addLayer(routeGraphicLayer);

              var facilities = new esri.tasks.FeatureSet();
              facilities = map.getLayer("featureLayerYard").graphics;
              facilities.features = facilities;

              params.facilities = facilities;
              params.outSpatialReference = map.spatialReference;
          });
          closestFacilityTask = new esri.tasks.ClosestFacilityTask("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Network/USA/NAServer/Closest Facility");
      }
      function clearGraphics() {
          dojo.byId("directionsDiv").innerHTML = "";
          map.graphics.clear();
          routeGraphicLayer.clear();
          incidentsGraphicsLayer.clear();
      }
      function mapClickHandler(evt) {
          clearGraphics();
          dojo.byId("directionsDiv").innerHTML = "";
          var inPoint = new esri.geometry.Point(evt.mapPoint.x, evt.mapPoint.y, map.spatialReference);
          var location = new esri.Graphic(inPoint);
          incidentsGraphicsLayer.add(location);

          var features = [];
          features.push(location);
          var incidents = new esri.tasks.FeatureSet();
          incidents.features = features;
          params.incidents = incidents;

          map.graphics.enableMouseEvents();

          dojo.connect(routeGraphicLayer, "onMouseOver", function (evt) {
              //clear existing directions and highlight symbol
              map.graphics.clear();

              dojo.byId("directionsDiv").innerHTML = "Hover over the route to view directions";

              var highlightSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 255, 255], 0.25), 4.5);
              var highlightGraphic = new esri.Graphic(evt.graphic.geometry, highlightSymbol);

              map.graphics.add(highlightGraphic);
              dojo.byId("directionsDiv").innerHTML = esri.substitute(evt.graphic.attributes, "${*}");

          });

          //solve 
          closestFacilityTask.solve(params, function (solveResult) {

              var directions = solveResult.directions;

              dojo.forEach(solveResult.routes, function (route, index) {
                  //build an array of route info
                  var attr = dojo.map(solveResult.directions[index].features, function (feature) {
                      return feature.attributes.text;
                  });
                  var infoTemplate = new esri.InfoTemplate("Attributes", "${*}");

                  route.setInfoTemplate(infoTemplate);
                  route.setAttributes(attr);

                  routeGraphicLayer.add(route);
                  dojo.byId("directionsDiv").innerHTML = "Hover over the route to view directions";
              });

              //display any messages
              if (solveResult.messages.length > 0) {
                  dojo.byId("directionsDiv").innerHTML = "<b>Error:</b> " + solveResult.messages[0];
              }
          });
      }
      dojo.addOnLoad(init);
  </script>
</head>
<body class="claro">      
  <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false" style="width:100%;height:100%;margin:0px;">
    <div id="map" dojotype="dijit.layout.ContentPane" region="center"class="panel"></div>    
    <div id="directions" dojotype="dijit.layout.ContentPane" region="bottom" class="panel" 
          style="height:250px;">
    <b>Click the map to find routes for the
    <select name="numLocations" dojotype="dijit.form.ComboBox" value="1" onChange="params.defaultTargetFacilityCount=this.value;clearGraphics();" style="width:60px;">
        <option selected="selected">1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </select> closest facilities to the input point.</b> 
    <div id="directionsDiv"></div>
    </div>
 </div>
</body>
</html>

Outcomes