Hide Table of Contents
View Geoprocessing - Service area task sample in sandbox
Geoprocessing - Service area task


This sample shows how you can send some input values to a GIS model on an ArcGIS Server and get the results back in your web application. In this example, the model calculates drive time polygons from a point that you click on the map. This type of analysis could be useful for a business that wanted to know how many people live within a 1, 2, and 3 minute drive, for example.

This model is exposed through an ArcGIS Server geoprocessing service. To get an idea of what inputs and outputs are used in the model, see the Services Directory page for the CalculateDriveTimePolygons service. Besides the default information, the model author has provided additional documentation that you can access by clicking the "Help URL" link on the page.

To access your models from ArcGIS Server, you'll need to create a geoprocessing task:

gp = new esri.tasks.Geoprocessor("...");

When you use a geoprocessing task, you'll typically configure a set of input parameters, then execute the task.

var params = {
"Input_Location": featureSet,
"Drive_Times": driveTimes
gp.execute(params, getDriveTimePolys);

You can optionally include a callback function that runs when the task is done executing. In this example the callback function getDriveTimePolys configures the symbols for the result polygons and adds them to the map.


<!DOCTYPE html>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">

  <title>Service Area Task</title>
  <link rel="stylesheet" href="https://js.arcgis.com/3.41/esri/css/esri.css">
    html, body, #mapDiv {
      height: 100%;
      margin: 0;
      padding: 0;
      width: 100%;

    #info {
      bottom: 20px;
      color: #444;
      height: auto;
      font-family: arial, sans-serif;
      left: 20px;
      margin: 5px;
      padding: 10px;
      position: absolute;
      text-align: left;
      width: 200px;
      z-index: 40;

  <script src="https://js.arcgis.com/3.41/"></script>
    ], function(Color, Map, Graphic, graphicsUtils, Geoprocessor, FeatureSet, SimpleMarkerSymbol, SimpleLineSymbol,
                SimpleFillSymbol) {

      var map, gp;
      var driveTimes = "1 2 3";

      // Initialize map, GP and image params
      map = new Map("mapDiv", {
        basemap: "topo-vector",
        center: [-95.255, 38.96],
        zoom: 11

      gp = new Geoprocessor("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons");
      gp.setOutputSpatialReference({wkid: 102100});
      map.on("click", computeServiceArea);

      function computeServiceArea(evt) {
        var pointSymbol = new SimpleMarkerSymbol();
        pointSymbol.setOutline = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 1);
        pointSymbol.setColor(new Color([0, 255, 0, 0.25]));

        var graphic = new Graphic(evt.mapPoint, pointSymbol);

        var features = [];
        var featureSet = new FeatureSet();
        featureSet.features = features;
        var params = { "Input_Location": featureSet, "Drive_Times": driveTimes };
        gp.execute(params, getDriveTimePolys);

      function getDriveTimePolys(results, messages) {
        var features = results[0].value.features;
        // add drive time polygons to the map
        for (var f = 0, fl = features.length; f < fl; f++) {
          var feature = features[f];
          if (f === 0) {
            var polySymbolRed = new SimpleFillSymbol();
            polySymbolRed.setOutline(new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 0, 0, 0.5]), 1));
            polySymbolRed.setColor(new Color([255, 0, 0, 0.7]));
          else if (f == 1) {
            var polySymbolGreen = new SimpleFillSymbol();
            polySymbolGreen.setOutline(new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                new Color([0, 0, 0, 0.5]), 1));
            polySymbolGreen.setColor(new Color([0, 255, 0, 0.7]));
          else if (f == 2) {
            var polySymbolBlue = new SimpleFillSymbol();
            polySymbolBlue.setOutline(new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 0, 0, 0.5]), 1));
            polySymbolBlue.setColor(new Color([0, 0, 255, 0.7]));
        // get the extent for the drive time polygon graphics and
        // zoom to the extent of the drive time polygons
        map.setExtent(graphicsUtils.graphicsExtent(map.graphics.graphics), true);
  <div id="mapDiv"></div>
  <div id="info" class="esriSimpleSlider">
    Click the map to use a Geoprocessing(GP) task to generate and zoom to drive time polygons. The drive time polygons
    are 1, 2, and 3 minutes.
Show Modal