auto populate field in Field maps from nearby feature?

06-08-2022 08:41 AM
Occasional Contributor II


I found this code from esri blog :

I am trying to get information of building specific BBL from MapPLUTO when they create or move point near the building

I get this code from above blog but I am getting this error 

Execution Error:Cannot create Geometry in this SpatialReference. Engine is using a different spatial reference.


// If feature doesn't have geometry return null
if (IsEmpty(Geometry($feature))) { return null }

// Get the parcels layer
var parcels = FeatureSetByName($map,"MapPLUTO")

// Buffer the current location and intersect with parcels
var bufferedLocation = Buffer($feature, 100, 'feet')
var candidateParcels = Intersects(parcels, bufferedLocation)

// Calculate the distance between the parcel and the current location
// Store the feature and distance as a dictionary and push it into an array
var featuresWithDistances = []
for (var f in candidateParcels) {
            'distance': Distance($feature, f, 'feet'),
            'feature': f

// Sort the candidate parcels by distance using a custom function
function sortByDistance(a, b) {
    return a['distance'] - b['distance']
var sorted = Sort(featuresWithDistances, sortByDistance)

// Get the closest feature
var closestFeatureWithDistance = First(sorted)

// If there was no feature, return null
if (IsEmpty(closestFeatureWithDistance)) { return null }

// Return the address
return `${closestFeatureWithDistance['feature']['ADDNUM']} ${closestFeatureWithDistance['feature']['ADDRESSNAM']}`


 also I found this solution from @XanderBakker but I don't know where I need to add it and how combine them to one code


function LatLonToMeters(lat, lon) {
    // convert from long/lat to google mercator (or EPSG:4326 to EPSG:900913)
    // Source:
    var x = lon * 20037508.34 / 180;
    var y = Log(Tan((90 + lat) * PI / 360)) / (PI / 180);
    y = y * 20037508.34 / 180;
    return [x, y];

var lon = -118.15;
var lat = 33.80;

var xy = LatLonToMeters(lat, lon);

var pointJSON = {'x' : xy[0], 'y' : xy[1], 'spatialReference' : {'wkid' : 102100}};
var pnt = Point(pointJSON);
return pnt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍






Tags (2)
0 Kudos
1 Reply
by Anonymous User
Not applicable

@anonymous55  this is due to a bug in form authoring. The feature and the map need to have the same spatial reference to perform geometry operations. Currently Field Maps web does not query the sample feature in the spatial reference of the map which leads to this issue. We have resolved this for our next release in a few weeks.

In the meantime, you can workaround it by making sure the layer and map (basemap) have the same spatial reference. This could mean re-publishing the feature layer using the Web Mercator projection.

0 Kudos