當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JavaScript ArcGIS Portal.createRouteTask用法及代碼示例


基本信息

以下是所在類或對象的基本信息。

AMD: require(["esri/portal/Portal"], (Portal) => { /* code goes here */ });

ESM: import Portal from "@arcgis/core/portal/Portal";

類: esri/portal/Portal

繼承: Portal > Accessor

自從:用於 JavaScript 4.0 的 ArcGIS API

用法說明

Portal.createRouteTask函數(或屬性)的定義如下:

createRouteTask () {Promise<RouteTask>}


自從:ArcGIS 適用於 JavaScript 4.12 的 API
已棄用 從 4.21 版本開始。請使用 route 和 helperServices 代替。

一個幫助函數,它返回門戶的 RouteTask helper service 的實例。

返回:

類型 說明
Promise<RouteTask> 解析後,返回 RouteTask 的實例。

例子:

// example using "esri/rest/route" with helperServices

require([
  "esri/portal/Portal",
  "esri/core/Collection",
  "esri/rest/route",
  "esri/rest/support/RouteParameters",
  "esri/rest/support/Stop",
  "esri/layers/GraphicsLayer",
  ...
], function(Portal, Collection, route, RouteParameters, Stop, GraphicsLayer, ... ) {

  // create new Portal object with relevant URL
  const portal = new Portal({
     url: "YOUR_PORTAL_URL"
  });

  // the stops and route result will be stored in this layer
  const routingLayer = new GraphicsLayer();

  // create a Collection of new Stops
  const stops = new Collection([
    new Stop({
      geometry: { x: -117.59275, y: 34.06200 },
      name: "Ontario Airport"
    }),
    new Stop({
      geometry: { x: -117.19570, y: 34.05609 },
      name: "Esri Campus"
    })
  ]);

  // setup the RouteParameters with API key and Stops
  const routeParams = new RouteParameters({
    // An authorization string used to access the routing service
    apiKey: "YOUR_API_KEY",
    stops
  });

  // define the symbology used to display the route
  const routeSymbol = {
    type: "simple-line", // autocasts as SimpleLineSymbol()
    color: [0, 0, 255, 0.5],
    width: 5
  };

  // load Portal instance
  portal.load().then(function() {
    // access helperServices from the Portal instance
    // to get the routing URL of interest
    const routeURL = portal.helperServices.route.url;
    // use helperServices to perform routing
    route.solve(routeURL, routeParams).then(showRouteInfo);
  }

  // do something useful with the results
  // like display them to the console
  // or display it on the map
  function showRouteInfo(routeSolveResult) {
    console.log("Show all results: ", routeSolveResult);
    console.log("Show the route information: ", routeSolveResult.routeResults[0].route);
    const routeResult = routeSolveResult.routeResults[0].route;
    routeResult.symbol = routeSymbol;
    routingLayer.add(routeResult);
    map.add(routingLayer);
  }
});

相關用法


注:本文由純淨天空篩選整理自arcgis.com大神的英文原創作品 Portal.createRouteTask。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。