ArcGIS API for javascript4.3——RouteTask

Posted on 2017-05-05 00:14  Y_Mathison  阅读(1282)  评论(2编辑  收藏  举报

功能实现:

RouteTask路径规划,在图层(GraphicsLayer)上添加停靠点,计算停靠点之间得路线,以LineSymbol实现路线展示

代码详解:

引用部分:

 

"esri/Map",
      "esri/views/MapView",
      "esri/Graphic",
      "esri/layers/GraphicsLayer",
      "esri/tasks/RouteTask",
      "esri/tasks/support/RouteParameters",
      "esri/tasks/support/FeatureSet",
      "esri/symbols/SimpleMarkerSymbol",
      "esri/symbols/SimpleLineSymbol",
      "esri/Color",
      "esri/core/urlUtils",
      "dojo/on",
      "dojo/domReady!"

route要素服务器:

 urlUtils.addProxyRule({
        urlPrefix: "route.arcgis.com",
        proxyUrl: "/sproxy/"
      });
  var routeTask = new RouteTask({
        url: "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World"
      });
urlUtils.addProxyRule避免引用服务器是出现登陆情况,图层来自

 关键代码:

var routeParams = new RouteParameters({
        stops: new FeatureSet(),
        outSpatialReference: { // autocasts as new SpatialReference()
          wkid: 3857
        }
      });
 new FeatureSet()获取feature,在通过feature获取Graphic数组,graphic里有symbol

   当地图被点击时,事件监听器调用函数addStop(),该函数在点击位置添加一个SimpleMarkerSymbol作为停止。该功能还在Route Parameter中添加点作为停止,并检查是否存在2个以上,路由通过调RouteTask.solve函数解决,然后将RouteParameter传递给求解函数。

 function addStop(event) {
   var stop = new Graphic({
          geometry: event.mapPoint,
          symbol: stopSymbol
        });
        routeLyr.add(stop);
   routeParams.stops.features.push(stop);
        if (routeParams.stops.features.length >= 2) {
          routeTask.solve(routeParams).then(showRoute);
        }
      }

showRoute回调函数获取结果对象中存储的routeResult,并对路由结果符号系统应用SimpleLineSymbol,然后将RouteResult添加到图形层中

 function showRoute(data) {
        var routeResult = data.routeResults[0].route;
        routeResult.symbol = routeSymbol;
        routeLyr.add(routeResult);
      }