JavaScript API 4.11 Sample Code

JavaScript API Geoprocessing - viewshed analysis


Note: Support for 3D on mobile devices may vary, view the system requirements for more information.

This sample demonstrates how to use the Geoprocessor to calculate a viewshed. Click any point on the map to see all areas that are visible within a 5 mile radius. It may take a few seconds for the model to run and send back the results.

The viewshed calculation is accomplished through an ArcGIS Server Geoprocessing service. The service provides access to a model (task) on the server that includes the viewshed tool. The constructor of the Geoprocessor requires the URL of the task. You can use the Services Directory to discover the URLs to your own Geoprocessing services and the tasks within them.

How it works

When the map is clicked, an event listener calls the function computeViewshed(), which adds a SimpleMarkerSymbol at the location of the click. The function also sets up the two parameters necessary for the task. The first parameter is the point clicked and the second parameter is the radius of the viewshed.

The next step is to pass the parameters to the Geoprocessor task. Then execute is called to perform the task synchronously. submitJob can also be called to run the task asynchronously. The execute method returns a promise which can be used with the .then() method to define a callback, in this case drawResultData.

var params = {
  Input_Observation_Point: featureSet,
  Viewshed_Distance: vsDistance

The drawResultData callback function obtains the features stored within the result object, iterates through them and applies a symbol to each graphic. The view then animates to the resulting array of graphics.

var resultFeatures = result.results[0].value.features;

// Assign each resulting graphic a symbol
var viewshedGraphics = {
  feature.symbol = fillSymbol;
  return feature;

// Add the resulting graphics to the graphics layer

  target: viewshedGraphics,
  tilt: 0

Sample search results