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


JavaScript ArcGIS serviceArea.solve用法及代碼示例


基本信息

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

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

ESM: import * as serviceArea from "@arcgis/core/rest/serviceArea";

對象: esri/rest/serviceArea

自從:用於 JavaScript 4.19 的 ArcGIS API

用法說明

serviceArea.solve函數(或屬性)的定義如下:

solve (url, params, requestOptions) {Promise<ServiceAreaSolveResult>}


根據一組參數確定服務區域。

參數:

類型說明
url String

表示網絡分析服務的 ArcGIS 服務器 REST 資源的 URL。

定義服務區域所需的參數。

requestOptions Object
可選的

用於數據請求的附加options(將覆蓋構造期間定義的 requestOptions)。

返回:

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

例子:

require([
  "esri/config",
  "esri/Map",
  "esri/views/MapView",
  "esri/rest/serviceArea",
  "esri/rest/support/ServiceAreaParameters",
  "esri/rest/support/FeatureSet",
  "esri/Graphic",
  ...
], function(esriConfig, Map, MapView, serviceArea, ServiceAreaParams, FeatureSet, Graphic, ...) {

  // API key from developer's account
  // https://developers.arcgis.com/documentation/mapping-apis-and-services/security/api-keys/
  // authenticates for the basemap and the serviceArea request
  esriConfig.apiKey = "YOUR_API_KEY";

  const map = new Map({
    basemap: "arcgis-newspaper"
  });

  const view = new MapView({
    container: "viewDiv",
    map: map,
    center: [-116.53818, 33.82586]
    zoom: 11
  });

  const serviceAreaUrl = "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";

  view.on("click", function(event){
    const locationGraphic = createGraphic(event.mapPoint);
    const serviceAreaParams = createServiceAreaParams(locationGraphic, view.spatialReference);
    solveServiceArea(serviceAreaUrl, serviceAreaParams);
  });

  // Create the location graphic
  function createGraphic(point) {
    view.graphics.removeAll();
    const graphic = new Graphic({
      geometry: point,
      symbol: {
        type: "simple-marker",
        color: "white",
        size: 8
      }
    });
    view.graphics.add(graphic);
    return graphic;
  }

  function createServiceAreaParams(locationGraphic, outSpatialReference) {
    // Create one or more locations (facilities) to solve for
    const featureSet = new FeatureSet({
      features: [locationGraphic]
    });

    // Set all of the input parameters for the service
    const serviceAreaParameters = new ServiceAreaParams({
      facilities: featureSet,
      trimOuterPolygon: true,
      outSpatialReference: outSpatialReference
    });
    return serviceAreaParameters;
  }

  function solveServiceArea(url, serviceAreaParams) {
    return serviceArea.solve(url, serviceAreaParams)
      .then(function(result){
        if (result.serviceAreaPolygons.length) {
          // Draw each service area polygon
          result.serviceAreaPolygons.forEach(function(graphic){
            graphic.symbol = {
              type: "simple-fill",
              color: "rgba(62,13,94,.25)"
            }
            view.graphics.add(graphic,0);
          });
        }
      }, function(error){
           console.log(error);
         });
  }
});

相關用法


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