Greetings,
I'd like to use a Survey123 form to extract a polygon from a hosted feature layer that intersects with the location of the submitted survey. A scenario would be " Given a total number of 5 districts when a surveyor opens the survey in any of them (with a geo-point question capturing his location), the geometry of this district gets drawn in a following geo-shape question".
I'm not a JS expert, but I edited the JS samples of the Survey123 connect templates and came up with the below code, but I can't get it to work. The JS function returns endless object (${element[1]} ${element[0]})
I understand that I'm not specific, but can anyone point out what is incorrect in the below code?
// Query a feature layer and returns the feature that intersects the location
function featureByLocation(layerURL, location, token) {
// Output value. Initially set to an empty string (XLSForm null)
let outValue = "";
let outRing = [];
// Check to make sure both layerURL and location are provided
if (layerURL == null || layerURL === "" || location == null || location === "") {
// The function can't go forward; exit with the empty value
return location;
}
// The coordinates will come in as `<lat> <lon> <alt> <acc>`.
// We need <lon>,<lat> for the query
// Note that I'm using the relatively new ` ` string that lets me place variables ${var}
let coordsArray = location.split(" ");
let coords = `${coordsArray[1]},${coordsArray[0]}`;
// Set up query parameters
let f = "f=json";
let returnGeometry = "returnGeometry=true";
let geometry = `geometry=${coords}`;
let geometryType = "geometryType=esriGeometryPoint";
let inSR = "inSR=4326";
let spatialRel = "spatialRel=esriSpatialRelIntersects";
let outFields = "outFields=*";
let returnCount = "returnCount=1";
let parameters = [f,returnGeometry, geometry,geometryType,inSR,spatialRel,outFields,,returnCount].join("&");
if (token) {
parameters = parameters + `&token=${token}`;
}
let url = `${layerURL}/query?${parameters}`;
// Create the request object
let xhr = new XMLHttpRequest();
// Make the request. Note the 3rd parameter, which makes this a synchronous request
xhr.open("GET", url, false);
xhr.send();
// Process the result
if (xhr.readyState === xhr.DONE) {
if (xhr.status !== 200) {
// The http request did not succeed
return "bad request: " + url
} else {
// Parse the response into an object
let response = JSON.parse(xhr.responseText);
if (response.error) {
// There was a problem with the query
} else {
if (response.features[0]) {
let ring = response["features"][0]["geometry"]["rings"][0];
ring.forEach(element => outRing.push('${element[1]} ${element[0]}'));
outValue = outRing.join(";");
} else {
// No features found
}
}
}
}
return outValue;
}
Hi Ahmed
do you find a solution ?
Thinks
Julien