Hi all,
I'm building a Survey123 form in Connect to collect habitat data. I want to automatically populate the "Reach Number" field based on the user's GPS location, using a polygon layer I’ve already created (each polygon represents a habitat reach zone).
Right now, I’m attempting to do this with a JavaScript file (zone_lookup.js) and the pulldata("@javascript") function. However, I’m getting the following error in the field app:
File not found: zone_lookup.js
Here’s what I’ve done so far:
I referenced the JS file in the XLSForm with pulldata("@javascript", "zone_lookup.js", "getZone", ${gps_location})
- Here is JS
function findZone(lat, lon, zones) {
for (var i = 0; i < zones.features.length; i++) {
var zone = zones.features[i];
var coordinates = zone.geometry.coordinates[0];
if (pointInPolygon([lon, lat], coordinates)) {
return zone.properties.reach_ID; // Adjust to your attribute
}
}
return "Unknown";
}
// Basic point-in-polygon function
function pointInPolygon(point, vs) {
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];
var intersect = ((yi > y) != (yj > y)) &&
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
Before I dive deeper, I’m wondering:
What’s the proper way to structure the zone_lookup.js file to query a polygon feature based on location?
Is there a better approach to this (like using pulldata("@layer")) if I already have a hosted feature layer with the polygon zones?
Will either of these methods work offline in the field app?
Thanks for any help — and I’m happy to share my XLSForm if needed.