lijunjiehdsx

ArcGIS Server 10.1 shortestRoute GP service javascript

Discussion created by lijunjiehdsx on Aug 25, 2013
Latest reply on Aug 26, 2013 by lijunjiehdsx
what is wrong?? or is something missing?

dojo.require("esri.map");
dojo.require("esri.tasks.geometry");
dojo.require("esri.toolbars.draw");
dojo.require("esri.tasks.query");

var map, routeTask,routeParams, routes = [];
var stopSymbol, barrierSymbol, routeSymbols;
var mapOnClick_addStops_connect, mapOnClick_addBarriers_connect;
//??????????????
function initMap(){
var lods = [
             {"level" : 0, "resolution" : 0.019035688046642237, "scale" : 8000000.0 },
             {"level" : 1, "resolution" :  0.009517844023321119, "scale" : 4000000.0},
             {"level" : 2, "resolution" : 0.004758922011660559, "scale" : 2000000.0},
             {"level" : 3, "resolution" : 0.0023794610058302797, "scale" : 1000000.0},
             {"level" : 4, "resolution" : 0.0011897305029151398, "scale" : 500000.0},
             {"level" : 5, "resolution" : 5.948652514575699E-4, "scale" : 250000.0},
             {"level" : 6, "resolution" : 2.9743262572878496E-4, "scale" : 125000.0},
             {"level" : 7, "resolution" : 1.522855043731379E-4, "scale" : 64000.0},
             {"level" : 8, "resolution" : 7.614275218656895E-5, "scale" : 32000.0},
           ];

map = new esri.Map("map",{
  lods:lods,
  zoom:2,
  extent: new esri.geometry.Extent({xmin:103.30029292030527,ymin:24.39151279999997,xmax:109.88812867358875,ymax:29.45420120000002,spatialReference:{wkid:4326}}),
  sliderStyle:"large",
  nav:true,
  navigationMode:"classic",
  showAttribution:true
});

     //?????????????????
var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://211.101.37.252:8399/arcgis/rest/services/guizhou4/MapServer");
map.addLayer(tiledMapServiceLayer);

    //????? GP??????
ShortestAnalyst = new esri.tasks.Geoprocessor("http://localhost:6080/arcgis/rest/services/shortroute/GPServer/ShortestRoute");

    //????GP???????????
     routeParams = new esri.tasks.RouteParameters();
     routeParams.stops = new esri.tasks.FeatureSet();
     routeParams.barriers= new esri.tasks.FeatureSet();
    
    //???????????????
  stopSymbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CROSS).setSize(15);
     stopSymbol.outline.setWidth(3);
     //????????????????
     barrierSymbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_X).setSize(10);
     barrierSymbol.outline.setWidth(3).setColor(new dojo.Color([255,0,0]));
     //????????????
     routeSymbols = new esri.symbol.SimpleLineSymbol().setColor(new dojo.Color([0,0,255,0.5])).setWidth(5);
    
   dojo.connect(ShortestAnalyst, "onSolveComplete", showRoute);
      dojo.connect(ShortestAnalyst, "onError", errorHandler);
}
 
dojo.ready(initMap);

//???????click???
function addStops() {
  removeEventHandlers();
  mapOnClick_addStops_connect = dojo.connect(map, "onClick", addStop);
}

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

//??????????
function addStop(evt) {
routeParams.stops.features.push(
    map.graphics.add(
      new esri.Graphic(
        evt.mapPoint,
        stopSymbol
       /* { RouteName:dojo.byId("routeName").value }*/
      )
    )
  );
}

//???????click???
function addBarriers() {
  removeEventHandlers();
  mapOnClick_addBarriers_connect = dojo.connect(map, "onClick", addBarrier);
}

//????????????????
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 addBarrier(evt) {
routeParams.barriers.features.push(
    map.graphics.add(
      new esri.Graphic(
        evt.mapPoint,
        barrierSymbol
      )
    )
  );
}

//????????????????????
function removeEventHandlers() {
  dojo.disconnect(mapOnClick_addStops_connect);
  dojo.disconnect(mapOnClick_addBarriers_connect);
}


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

//????????????
function RouteAnalyst() {
         /*if (mapOnClick_addStops_connect.features.length == 0 ||mapOnClick_addBarriers_connect.features.length==0)
         {
          alert("???????????????????????????");
          return;
          } */
         clearRoutes();
         //String gpid="guizhou_ND";
         var gpParams = { "Input_locations ": routeParams.stops, "Input_Barriers": routeParams.barriers,};
         ShortestAnalyst.submitJob (gpParams, showRoute,errorHandler);//???
        }
      
//?????????????
function showRoute(results, messages) {
         var features = results[0].value.features;
         for(var i = 0;i < features.length;i++)
         {
          var graphic = features[i];
          graphic.setSymbol(routeSymbols);
          Map.graphics.add(graphic);  
         }
}
//???????
function errorHandler(err) {
  alert("An error occured\n" + err.message + "\n" + err.details.join("\n"));
}

Outcomes