# Calculate distance to closest polling station in pop up using arcade

931
1
09-09-2020 02:18 PM
New Contributor

Hello all,

I am trying to write an expression to relay in a pop up how far away a selected point (a polling location) to a specific point(a small town) in miles. I have tried several times with a distance expression but I can't seem to get it written right. I'm new to arcade, so I may not be grasping what I am missing.

var Geometry="City"

Tags (3)
Esri Esteemed Contributor

Hi Marvin Lopez ,

There is no function in Arcade that will get you the nearest feature in another layer. This does not mean that it is not possible. Let's assume you have two point layers, one holding the towns and the other the polling stations. When the user clicks on a town you will have access to the geometry. The town does not necessarily have to be a point, it could also be a polygon. If you have a huge amount of polling stations you will want to limit the search to a certain distance. This could be your search tolerance. The Arcade expression will take the town geometry (being a point or a polygon) and buffer it with the search tolerance. The resulting buffer polygon will be used to intersect the polling stations layer to obtain only the polling stations within the search distance. Next step would be to loop through each polling station and determine the distance and remember the minimum distance. In the resulting text that you can present to the end user you can include the name of the nearest polling station and the "as the crow flies" distance between the two. Remember, this will not be the actual distance someone will have to travel to get there.

See the expression below how to do this:

``````// specify a search distance using the map units
var searchtol = 10000;
// the town clicked on by the user
var town = \$feature;
// buffer the town with search distance and specify the units
var searchbuffer = Buffer(town, searchtol, 'meters');
// access the poling stations layer (change according to your data)
var pollingstations = FeatureSetByName(\$map, 'SpecifyTheNameOfThePollingStationsLayerInTheMap', ['FieldnamePolingStation']);
// Find the nearpolling stations that are within the search distance
var nearstations = Intersects(pollingstations, searchbuffer);
// count polling stations in buffer
var cnt = Count(nearstations);
// check if there are any stations
if (cnt > 0) {
// there are polling stations
// initialize minimum distance, and polling station name
var min_dist = searchtol + 1;
var polstatname = null;
// loop through polling stations
for (var polstat in nearstations) {
// calculate distance between town and polling station
var dist = Distance(polstat, town, 'meters');
// if nearer than prevous nearest
if (dist < min_dist) {
// update nearest
min_dist = dist;
polstatname = polstat['FieldnamePolingStation'];
}
}
// return polling station name and distance to it
return polstatname + " (" + Round(min_dist, 1) + "m.)";

} else {
// there are NO polling stations (change units)
return "No polling stations within " + searchtol + " m.";
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````