//variable that calls the textbox value var type = document.getElementById("textbox").value; //Set up the query per type var queryTask = new esri.tasks.QueryTask("http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/Incident_Data_Extraction/MapServer/2"); var query = new esri.tasks.Query(); query.returnGeometry = true; //field to query query.outFields = ["ftype"]; //execute query function executeQueryTask(type) { query.where = "ftype = " + type; //If you want a result handler function, just replace that here queryTask.execute(query, function(featureSet){ //This is designed to get the FIRST feature graphic as a JSON in the return because I am assuming you only have one county with that name var AOI = featureSet.features[0].geometry.toJSON(); }); } //First Area of interest selected by the client var Area = new FeatureSet(); var features = []; features.push(map.graphics.graphics[0]); Area.features = features; if (executeQueryTask) { var params = { "Layers_to_Clip": clipLayers, "Area_of_Interest": AOI, "Feature_Format": registry.byId("formatBox").get("value") }; } else { var params = { "Layers_to_Clip": clipLayers, "Area_of_Interest": Area, "Feature_Format": registry.byId("formatBox").get("value") }; }
queryTask.execute(query, function(featureSet){ var Area = new FeatureSet(); var features = []; features.push(map.graphics.graphics[0]); Area.features = features; var params = { "Layers_to_Clip": clipLayers, "Feature_Format": registry.byId("formatBox").get("value") }; if (featureSet.features.length > 0) { var AOI = featureSet.features[0].geometry.toJSON(); params.Area_of_Interest = AOI; } else { params.Area_of_Interest = Area; } // now do something with "params" here });
I'm not familiar with the sample you referenced, but after a quick look at your code and I think there are few issues here...
1) You need to include the brackets to call a function. So where you have "if (executeQueryTask)", this should read "if (executeQueryTask())" if you want it to call your function. As it stands, it will always return true.
2) executeQueryTask() returns nothing, so if you do call it from your if statement, it will always return false.
3) queryTask.execute() is asynchronous, so if you do call executeQueryTask in your "if" statement, it will not wait queryTask.execute() to complete before returning.
4) You are declaring AOI within the scope of the callback function on queryTask.execute(). It will not be accessible in other areas of your code. You should declare this outside the callback function and then assign it within the callback function.
I think what you probably want to do is include all that code inside the call back function, so the callback function will look something like thisqueryTask.execute(query, function(featureSet){ var Area = new FeatureSet(); var features = []; features.push(map.graphics.graphics[0]); Area.features = features; var params = { "Layers_to_Clip": clipLayers, "Feature_Format": registry.byId("formatBox").get("value") }; if (featureSet.features.length > 0) { var AOI = featureSet.features[0].geometry.toJSON(); params.Area_of_Interest = AOI; } else { params.Area_of_Interest = Area; } // now do something with "params" here });
edit: Actually, there is still a question of what you want params.Area_of_Interest to actually be? In one case you are setting it to be a geometry string with: featureSet.features[0].geometry.toJson()... in the other case you are setting it to be a FeatureSet.
queryTask.execute(query, function(featureSet){ var Area = new FeatureSet(); var features = []; features.push(map.graphics.graphics[0]); Area.features = features; var params = { "Layers_to_Clip": clipLayers, "Feature_Format": registry.byId("formatBox").get("value") }; if (featureSet.features.length > 0) { var AOI = featureSet.features[0].geometry; params.Area_of_Interest = AOI; } else { params.Area_of_Interest = map.graphics.graphics[0].geometry; } // now do something with "params" here });
You just need to be careful because you define "Area" as a FeatureSet and assign that to params.Area_of_Interest, but you also assign featureSet.features[0].geometry.toJson() to params.Area_of_Interest and this is a geometry.
You should decide which type you want to assign, a FeatureSet or a geometry.
Probably what you want is just something like thisqueryTask.execute(query, function(featureSet){ var Area = new FeatureSet(); var features = []; features.push(map.graphics.graphics[0]); Area.features = features; var params = { "Layers_to_Clip": clipLayers, "Feature_Format": registry.byId("formatBox").get("value") }; if (featureSet.features.length > 0) { var AOI = featureSet.features[0].geometry; params.Area_of_Interest = AOI; } else { params.Area_of_Interest = map.graphics.graphics[0].geometry; } // now do something with "params" here });
Also, it doesn't look like you're waiting for your DOM and all of your widgets/components to finish loading before you start trying to access them.
I usually just use data-dojo-config="praseOnLoad: true" to do this to make sure the document gets parsed when the page loads and then wrap my loading function inside a dojo/ready which makes sure that all the required widgets have laoded. See here and here for more info on this.
It would look something like this (I took out your TOC, but you should be able to add it back in when you run it in your environment).
http://jsbin.com/ficetiwo/1/edit
parser.parse();was parsing on load. Also I was unaware of the module
"dojo/ready". So thank you!
if ( clipLayers.length === 0 || map.graphics.graphics.length === 0 ) { alert("Select layers to extract and draw an area of interest."); return;