I am trying to create an Arcade script that, within the pop-up, will return the next closest feature within the same layer. In my case, if I click on a wildfire point, it will tell me what the next closest wildfire point is. I have has some success changing up the script found in lines 30-44 here, but what I have found is that my current script returns the point that is furthest from the selected point within the search distance. Any guidance on what I can do?
var searchDistance =50;
var points = $layer
var closestPoint = Intersects(points, BufferGeodetic($feature, searchDistance, "Miles"));
var minDistance = Infinity
for(var listing in closestPoint){
var facilityDistance = Distance(listing, $feature, "miles");
if (facilityDistance < minDistance){
closestPoint = listing;
return closestPoint.iaName
}
};