HOW TO

Display the distance of the nearest point feature using an Arcade expression in Portal for ArcGIS and ArcGIS Online Map Viewer pop-ups

Last Published: March 29, 2023

Summary

At times, it is necessary to determine the distance of the nearest point feature from a different point hosted feature layer. For example, the image below shows shopping malls (represented by the purple point features) and train stations (represented by the yellow point features) in a map. The distance from the train station to the shopping mall is determined to inform the public transport users who may opt to walk to the shopping mall from the nearest train station. This distance is populated using an Arcade expression and is displayed on the web map pop-up.

The shopping malls and train stations represented by point features in a map.

Procedure

  1. Open the web map in Map Viewer.
  2. On the Contents (dark) toolbar, click Layers The Layers icon..
  3. In the Layers pane, click the hosted feature layer.
  4. On the Settings (light) toolbar, click Pop-ups The Pop-ups icon..
  5. In the Pop-ups pane, under Options, click Manage expressions > Add expression.
Note: 
For ArcGIS Online, in the Pop-ups pane, click Attribute expressions > Add expression.
  1. In the dialog box, specify a name for the expression.
  2. In the Expression box, copy and paste the following expression.
    1. Set the necessary variables for the feature layer. Replace <Point_Layer_2> with the other point feature layer, <value> with the maximum distance between the point feature layers and <the unit of measurement> with the desired unit of measurement.
var fs = FeatureSetByName($map,"<Point_Layer_2>");
var buf = Buffer($feature, <value>, "<the unit of measurement>");
var fs_filtered = Intersects(fs, buf);
var cnt = Count(fs_filtered);
var min_dist = 999;
  1. Calculate the distance from one point feature to another using the Distance() function.
if (cnt > 0) {
    for (var f in fs_filtered) {
        var dist = Distance($feature, f);
        if (dist < min_dist) {
            min_dist = dist;
        }
    }
}

return min_dist;
  1. Click Test.

The following code block demonstrates the full Arcade expression.

var fs = FeatureSetByName($map,"PointFeature2");
var buf = Buffer($feature, 400, "meter");
var fs_filtered = Intersects(fs, buf);
var cnt = Count(fs_filtered);
var min_dist = 999;

if (cnt > 0) {
    for (var f in fs_filtered) {
        var dist = Distance($feature, f);
        if (dist < min_dist) {
            min_dist = dist;
        }
    }
}

return min_dist;
The Arcade expression configured in the expression box.
Note: 
For ArcGIS Online, after copying and pasting the Arcade expression in the expression box, click Run.
  1. When the Results tab displays the distance, click OK.
The nearest distance of the other point feature displayed.
Note: 
For ArcGIS Online, when the Output tab displays the distance, click Done.
  1. Click the Back The Back icon. button next to Pop-up expressions or Attribute expressions (for ArcGIS Online).
  2. In the Pop-ups pane, click Fields list > Select fields.
  3. In the Select fields pane, under Expressions, check the newly created expression field, and click Done.
The expression field checked under Expressions in the Select fields pane.

The image below shows the distance between a shopping mall and the nearest train station is 384.20553.

The nearest distance displayed in the pop-up.

Article ID: 000029737

Software:
  • ArcGIS Online
  • Portal for ArcGIS

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options