AnsweredAssumed Answered

route can't display in web

Question asked by daihere on Aug 6, 2015
Latest reply on Aug 6, 2015 by rscheitlin

Hi, all:

     Now,Im studying a function about shortest route and I encounter a problem.

     I released by arcmap successful network analyze service.

     When I try to generate any route, the route is not show in the graphic layer.

     When the code is debug to see where is the error, it does't show any problem, and looks all ok.

     Can someone give me and advice on where the problem can be???

 

     Here is the js code:

var editToolbar;

var currentLocation

require(["esri/map", "esri/geometry/Point", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/toolbars/draw", "esri/toolbars/edit",

  "esri/symbols/SimpleMarkerSymbol","esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol",

  "esri/symbols/PictureMarkerSymbol", "esri/graphic", "esri/geometry/jsonUtils", "esri/tasks/RouteTask", "esri/tasks/FeatureSet",

  "esri/tasks/RouteParameters", "esri/Color", "dojo/parser", "dijit/Menu",

  "dijit/MenuItem", "dijit/MenuSeparator", "dijit/form/Button",

  "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!"],

  function (

  Map, Point, ArcGISDynamicMapServiceLayer, Draw, Edit, SimpleMarkerSymbol, SimpleLineSymbol,

  SimpleFillSymbol, PictureMarkerSymbol, Graphic, geometryJsonUtils, RouteTask, FeatureSet, RouteParameters, Color, parser, Menu,

  MenuItem, MenuSeparator

  ) {

  var routes = [];

  var flag1, flag2 = false;

  var originPrve, pathwayPrve, endPrve;

  var graphic1, graphic2, graphic3;

  var flag = 0, x = 0, y = 0;

  var editToolbar;

 

 

  parser.parse();

 

 

  var map = new Map("mapDiv");

 

 

  map.setLevel(2);

  var url = "http://localhost:6080/arcgis/rest/services/china1/MapServer";

  var agoLayer = new ArcGISDynamicMapServiceLayer(url);

  // var agoLayer = new esri.layers.ArcGISTiledMapServiceLayer(url);

  map.addLayer(agoLayer);

 

 

  routeTask = new RouteTask("http://localhost:6080/arcgis/rest/services/ttt/NAServer/Route");

        routeParams = new RouteParameters();

        routeParams.stops = new FeatureSet();

 

 

        routeSymbol = new SimpleLineSymbol().setColor(new Color([255, 0, 0, 0.5])).setWidth(5);

 

 

        map.on("mouse-down", pointMove);

  map.on("load", createToolbarMenu);

 

 

 

  //使各个点移动结束后触发路径的再建立

  function pointMove (evt) {

  if (originPrve) {

  editToolbar.on("graphic-move-stop", function (originPrve) {

  var num = 0;

  clearRoutes();

 

 

  for (var i = routeParams.stops.features.length-1; i>=0; i--) {

  if (routeParams.stops.features[i].symbol.url === "originPoint.png") {

  num++;

  }

  if (routeParams.stops.features[i].symbol.url === "endPoint.png") {

  num ++;

  }

  }

 

 

  if (num == 2){

  routeTask.solve(routeParams, showRoute, errorHandler);

  }

  });

  }

 

 

  if (pathwayPrve) {

  editToolbar.on("graphic-move-stop", function (pathwayPrve) {

  var num = 0;

  clearRoutes();

 

 

  for (var i = routeParams.stops.features.length-1; i>=0; i--) {

  if (routeParams.stops.features[i].symbol.url === "originPoint.png") {

  num++;

  }

  if (routeParams.stops.features[i].symbol.url === "endPoint.png") {

  num ++;

  }

  }

 

 

  if (num == 2){

  routeTask.solve(routeParams, showRoute, errorHandler);

  }

  });

  }

 

 

  if (endPrve) {

  editToolbar.on("graphic-move-stop", function (endPrve) {

  var num = 0;

  clearRoutes();

 

 

  for (var i = routeParams.stops.features.length-1; i>=0; i--) {

  if (routeParams.stops.features[i].symbol.url === "originPoint.png") {

  num++;

  }

  if (routeParams.stops.features[i].symbol.url === "endPoint.png") {

  num ++;

  }

  }

 

 

  if (num == 2){

  routeTask.solve(routeParams, showRoute, errorHandler);

  }

  });

  }

  }

 

 

  function createToolbarMenu () {

  editToolbar = new Edit(map);

 

 

  createMapMenu();

  }

 

 

  function createMapMenu() {

  var ctMenuForMap = new Menu({

  onOpen: function (box) {

  currentLocation = getMapPointFromMenuPosition(box);

  }

  });

 

 

  ctMenuForMap.addChild(new MenuItem({

  label: "设为起点",

  onClick: function (evt) {//起点只 能有一个

  var num = 0;

 

  if (flag1) {

  for (var i = routeParams.stops.features.length-1; i>=0; i--) {

  if (routeParams.stops.features[i].symbol.url === "originPoint.png") {

  routeParams.stops.features.splice(i, 1)[0];

  }

  }

 

 

  map.graphics.remove(originPrve)

  clearRoutes();

  }

  var symbol = new PictureMarkerSymbol({

  "url": "originPoint.png",

  "height": 63,

  "width": 45,

  "type": "esriPMS"

  });

  var graphic = new Graphic(geometryJsonUtils.fromJson(currentLocation.toJson()), symbol);

  routeParams.stops.features.push(

  map.graphics.add(graphic)

  );

 

 

  originPrve = graphic;

  editToolbar.activate(Edit.MOVE, originPrve);

 

 

 

  flag1 = true;

 

 

  //只有在起点和终点都设立之后才可以生成路径

  for (var i = routeParams.stops.features.length-1; i>=0; i--) {

  if (routeParams.stops.features[i].symbol.url === "originPoint.png") {

  num++;

  }

  if (routeParams.stops.features[i].symbol.url === "endPoint.png") {

  num ++;

  }

  }

 

 

  if (num == 2){

  routeTask.solve(routeParams, showRoute, errorHandler);

  }

  }

  }));

 

 

  ctMenuForMap.addChild(menuItem = new MenuItem({

  label: "设为途径点",

  onClick: function (evt) {

  var num = 0;

  flag ++; //falg为全局变量,目的是控制途径点的个数

  var symbol;

 

 

  clearRoutes();

  symbol = new PictureMarkerSymbol({

  "width": 45,

  "height": 69,

  "type": "esriPMS"

  });

  switch (flag) {

  case 1:

  symbol.url = "pathwayPoint1.png";

  break;

  case 2:

  symbol.url = "pathwayPoint2.png";

  break;

  case 3:

  symbol.url = "pathwayPoint3.png";

  break;

  case 4:

  symbol.url = "pathwayPoint4.png";

  break;

  case 5:

  symbol.url = "pathwayPoint5.png";

  menuItem.disabled = true;

  break;

  }

  var graphic = new Graphic(geometryJsonUtils.fromJson(currentLocation.toJson()), symbol);

  routeParams.stops.features.push(

  map.graphics.add(graphic)

  );

  editToolbar.activate(Edit.MOVE, graphic);

  pathwayPrve = graphic;

 

 

  for (var i = routeParams.stops.features.length-1; i>=0; i--) {

  if (routeParams.stops.features[i].symbol.url === "originPoint.png") {

  num++;

  }

  if (routeParams.stops.features[i].symbol.url === "endPoint.png") {

  num ++;

  }

  }

 

 

  if (num == 2){

  routeTask.solve(routeParams, showRoute, errorHandler);

  }

 

  }

  }));

 

 

  ctMenuForMap.addChild(new MenuItem({

  label: "设为终点",

  onClick: function (evt) {

  var num = 0;

 

 

  if (flag2) {//终点只能有一个

  for (var i = routeParams.stops.features.length-1; i>=0; i--) {

  if (routeParams.stops.features[i].symbol.url === "endPoint.png") {

  routeParams.stops.features.splice(i, 1)[0];

  }

  }

  map.graphics.remove(endPrve)

  clearRoutes();

  }

 

  var symbol = new PictureMarkerSymbol({

  "url": "endPoint.png",

  "width": 45,

  "height": 61,

  "type": "esriPMS"

 

 

  });

  var graphic = new Graphic(geometryJsonUtils.fromJson(currentLocation.toJson()), symbol);

  routeParams.stops.features.push(

  map.graphics.add(graphic)

  );

  editToolbar.activate(Edit.MOVE, graphic);

 

 

  endPrve = graphic;

  flag2 = true;

 

 

  for (var i = routeParams.stops.features.length-1; i>=0; i--) {

  if (routeParams.stops.features[i].symbol.url === "originPoint.png") {

  num++;

  }

  if (routeParams.stops.features[i].symbol.url === "endPoint.png") {

  num ++;

  }

  }

  if (num == 2){

  routeTask.solve(routeParams, showRoute, errorHandler);

  }

  }

  }));

 

 

  ctMenuForMap.startup();

  ctMenuForMap.bindDomNode(map.container);

  }

 

 

  function getMapPointFromMenuPosition (box) {

  var x = box.x, y = box.y;

  var screenPoint = new Point(x - map.position.x, y - map.position.y);

  return map.toMap(screenPoint);

  }

 

 

  function showRoute (result) {

 

 

  var routeResults = result.routeResults;

  routes.push(

  map.graphics.add(routeResults[0].route.setSymbol(routeSymbol))

  );

  console.log(routeResults[0].route.setSymbol(routeSymbol));

  var msgs = ["服务器消息:"];

     for (var i = 0; i < result.messages.length; i++) {

         msgs.push(result.messages[i].type + " : " + result.messages[i].description);

     }

     if (msgs.length > 1) {

         alert(msgs.join("\n - "));

     }

  }

 

 

 

 

  function clearRoutes() {

    for (var i = routes.length - 1; i >= 0; i--) {

        map.graphics.remove(routes.splice(i, 1)[0]);

    }

    routes = [];

  }

 

 

  function errorHandler(err) {

    alert("发生错误\n" + err.message + "\n" + err.details.join("\n"));

  }

 

 

  })

Outcomes