Select to view content in your preferred language

Arcade Expression works in Map Viewer but not Field Maps

1976
10
03-04-2024 04:05 PM
ChrisJRoss13
Occasional Contributor

I have an arcade expression that works perfectly fine in Map Viewer but when I access the map in Field Maps it always returns "No closest weather station was found within the search distance".

Any help on why this is would be greatly appreciated.

The expression is below:

var searchDistance = 50; // Search distance in kilometers
var weatherStations = FeatureSetByPortalItem(Portal('https://www.arcgis.com'),
  '38406bada3104e258fc8b42a2a96581a',
  0,
  ['Temperature_C'],
  True
);
var proximityWeatherStations = Intersects(weatherStations, Buffer($feature, searchDistance, "kilometers"));
var minDistance = Infinity;
var closestWeatherStationTemp;

for (var f in proximityWeatherStations){
 var weatherDist = Round(Distance(f, $feature, "kilometers"),2);
 if (weatherDist < minDistance) {
     closestWeatherStationTemp = f["Temperature_C"];
     minDistance = weatherDist;
 }
}

// Check if a closest individual weather station was found before accessing its property
if (closestWeatherStationTemp != null) {
    return "Closest Weather Station Temperature is: " + closestWeatherStationTemp + " degrees C and is located approximately " + Round(minDistance,0) + " km away.";
} else {
    return "No closest weather station was found within the search distance.";
}
10 Replies
jcarlson
MVP Esteemed Contributor

Are you offline when you use Field Maps? This expression would require access to the internet in order to get information from the weatherStations featureset.

- Josh Carlson
Kendall County GIS
0 Kudos
ChrisJRoss13
Occasional Contributor

Yes, all online. I am aware that when you move offline that this functionality does not work.

0 Kudos
jcarlson
MVP Esteemed Contributor

Curious. I don't see anything obvious in the expression otherwise. We use "FeatureSetByPortalID" functions in Field Maps without issue, but it may be worth adding the layer to the map and testing @DougBrowning 's suggestion just to see.

Are you testing in Field Maps with the same login as you use in the Map Viewer? Could it be a sharing permission set on the absent layer?

- Josh Carlson
Kendall County GIS
0 Kudos
DougBrowning
MVP Esteemed Contributor

I think it is from using FeatureSetbyProtalID.  Change to FeatureSetByName and use the name from the map.  See if that does it.

0 Kudos
ChrisJRoss13
Occasional Contributor

The actual hosted feature layer is not contained in the map therefore I cannot use the FeatureSetByName. I have another expression that pulls property information (not contained in the map) that uses FeatureSetByPortalID and it works fine.

0 Kudos
DougBrowning
MVP Esteemed Contributor

Ok next idea is this line  (closestWeatherStationTemp != null)   change to isEmpty   Not sure just null works?

0 Kudos
jcarlson
MVP Esteemed Contributor

Checking null works, at least in other contexts. Wouldn't be the first time there was a notable discrepancy between one version of Arcade and another, though.

- Josh Carlson
Kendall County GIS
0 Kudos
DougBrowning
MVP Esteemed Contributor

Not sure what declaring a var with no value does even?  It would not even have a type?  Maybe set it to '' instead then check that.

I would use Console and see what closestWeatherStationTemp is getting.  

0 Kudos
jcarlson
MVP Esteemed Contributor

Correct, it's just a null, no type. Makes it easier to later just assign values without declaring it over and over inside the loop.

- Josh Carlson
Kendall County GIS
0 Kudos