I am trying to use attribute rules to write in the distance to a nearest neighbor, I've been able to get the nearest neighbors name to show up, however the distance alludes me. Here is the code I was using, however when add an area it seems to pick up the 3rd closest neighbor and not the nearest, can anyone help me figure out what I am doing wrong.
var NeighborLayer = FeatureSetByName($datastore, "TestNeighbors", ["NAME"]);
var searchDistance = 10000;
var NeighborIntersect = Intersects(NeighborLayer, Buffer($feature, searchDistance, "feet"));
var cnt = Count(NeighborIntersect);
var minDistance = 10000;
var name = Null
if (cnt > 0) {
for (var Neighbor in NeighborIntersect) {
var dist = Distance(Neighbor, $feature, "feet");
}
} else {
// pass no features found within search distance, name remains null
}
return dist; 