QueryTask

894
6
01-04-2013 10:58 AM
ToddMcNeil1
New Contributor
I have an asp.net page with a map and two controls.  One control selects a state, populates the second control with the counties within that selected state.  I select multiple counties and hit a button.  The button fires of a javascript function, loopSelected(), to loop through the selected counties and create an array.

function loopSelected() {
    //Define an array to store selected counties
    var selectedArray = new Array();
    var selObj = document.getElementById('countyListBox');
    var i;
    var count = 0;
    for (i = 0; i < selObj.options.length; i++) {
        if (selObj.options.selected) {
            selectedArray[count] = selObj.options.value;
            count++;
        }
    }
    //Pass array to Query and Find functions
    runCountyQuery(selectedArray);
    doCountyFind();
}


The selectedArray is passed to another function, runCountyQuery, that performs the QueryTask to select the multiple counties.

function runCountyQuery(selectedArray) {

    //Define a new Query and QueryTask
    queryTask = new esri.tasks.QueryTask("http://hwvanad1287/ArcGIS/rest/services/tdat/Acs/MapServer/0");

    //Create query parameters
    query = new esri.tasks.Query();
    query.returnGeometry = true;
    query.outFields = ["*"];

    //Parse selectedArray for query.where clause
    var strText;
    var i;
    var count = 0;
    for (i = 0; i < selectedArray.length; i++) {
        strText = selectedArray;
        alert('strText[' + i + ']: ' + strText);
        count++;
    }

    //This section actually runs the query. It's called by the loopSelected() function above.
    //The query is run as a deferred process, and the results are returned to the showQueryResults function.

    //The query's where clause is specified in the input field.
    //query.where = document.getElementById("countyListBox").value;
    //query.where = "GEOID = " + "'" + query.where + "'";
    //query.where = "GEOID IN (" + "'13001','13003','13005'" + ")";
    query.where = "GEOID IN (" + selectedArray + ")";
    alert('query.where:' + query.where);
    queryTask.execute(query, showQueryResults, errResults);
}


My problem is building the proper syntax for the query.where clause from the selectedArray.  I would also like to find the easiest way to debug the results on execute.  If there is an easier way to construct a query.where clause containing multiple values, I am all ears.

Todd McNeil
Geographer

U.S. Department of Housing and Urban Development
Office of Policy Development and Research
0 Kudos
6 Replies
HemingZhu
Occasional Contributor III
I have an asp.net page with a map and two controls.  One control selects a state, populates the second control with the counties within that selected state.  I select multiple counties and hit a button.  The button fires of a javascript function, loopSelected(), to loop through the selected counties and create an array.

function loopSelected() {
    //Define an array to store selected counties
    var selectedArray = new Array();
    var selObj = document.getElementById('countyListBox');
    var i;
    var count = 0;
    for (i = 0; i < selObj.options.length; i++) {
        if (selObj.options.selected) {
            selectedArray[count] = selObj.options.value;
            count++;
        }
    }
    //Pass array to Query and Find functions
    runCountyQuery(selectedArray);
    doCountyFind();
}


The selectedArray is passed to another function, runCountyQuery, that performs the QueryTask to select the multiple counties.

function runCountyQuery(selectedArray) {

    //Define a new Query and QueryTask
    queryTask = new esri.tasks.QueryTask("http://hwvanad1287/ArcGIS/rest/services/tdat/Acs/MapServer/0");

    //Create query parameters
    query = new esri.tasks.Query();
    query.returnGeometry = true;
    query.outFields = ["*"];

    //Parse selectedArray for query.where clause
    var strText;
    var i;
    var count = 0;
    for (i = 0; i < selectedArray.length; i++) {
        strText = selectedArray;
        alert('strText[' + i + ']: ' + strText);
        count++;
    }

    //This section actually runs the query. It's called by the loopSelected() function above.
    //The query is run as a deferred process, and the results are returned to the showQueryResults function.

    //The query's where clause is specified in the input field.
    //query.where = document.getElementById("countyListBox").value;
    //query.where = "GEOID = " + "'" + query.where + "'";
    //query.where = "GEOID IN (" + "'13001','13003','13005'" + ")";
    query.where = "GEOID IN (" + selectedArray + ")";
    alert('query.where:' + query.where);
    queryTask.execute(query, showQueryResults, errResults);
}


My problem is building the proper syntax for the query.where clause from the selectedArray.  I would also like to find the easiest way to debug the results on execute.  If there is an easier way to construct a query.where clause containing multiple values, I am all ears.

Todd McNeil
Geographer

U.S. Department of Housing and Urban Development
Office of Policy Development and Research


// not return array, return a string instead like this ('13001','13002','13003'...)
function loopSelected() {
    //Define an array to store selected counties
    var  selectedArray ="(";
    var selObj = document.getElementById('countyListBox');
    var i;
    var count = 0;
    for (i = 0; i < selObj.options.length; i++) {
        if (selObj.options.selected) {
            if (selectedArray =="(")
            {
                selectedArray +="'"+ selObj.options.value+"'";
             }
             else{
                 selectedArray +=",'"+ selObj.options.value+"'";       
             }
            count++;
        }
    }
     selectedArray +=")";
    //Pass this string to Query and Find functions
    runCountyQuery(selectedArray);
    doCountyFind();
}
0 Kudos
ToddMcNeil1
New Contributor
hzhu,

It would be easier and not sure why I had to make the problem more difficult by using an array.  🙂  I think i wanted to see if I could do it.

I also would like to zoom to the map extent of all the selected features.  I read somewhere that I would have to union all features together then get the extent.  I have the code below and could use some assistance.

function showQueryResults(results) {
    
var thisExtent, fullExtent;
for (var i = 0, il = results.features.length; i < il; i++) {
  fullExtent = results.features.geometry.getExtent();
  fullExtent.union(fullExtent);
}

map.setExtent(fullExtent.getExtent().expand(1.5));

}


This seems like it is close but the fullExtent is only storing the extent of the last selected feature.

Thanks,

Todd McNeil
Geographer

U.S. Department of Housing and Urban Development
Office of Policy Development and Research
0 Kudos
HemingZhu
Occasional Contributor III
hzhu,

It would be easier and not sure why I had to make the problem more difficult by using an array.  🙂  I think i wanted to see if I could do it.

I also would like to zoom to the map extent of all the selected features.  I read somewhere that I would have to union all features together then get the extent.  I have the code below and could use some assistance.

function showQueryResults(results) {
    
var thisExtent, fullExtent;
for (var i = 0, il = results.features.length; i < il; i++) {
  fullExtent = results.features.geometry.getExtent();
  fullExtent.union(fullExtent);
}

map.setExtent(fullExtent.getExtent().expand(1.5));

}


This seems like it is close but the fullExtent is only storing the extent of the last selected feature.

Thanks,

Todd McNeil
Geographer

U.S. Department of Housing and Urban Development
Office of Policy Development and Research

function showQueryResults(results) {
map.extent =esri.graphicsExtent(results.features).expand(1.5);
//or map.setExtent(esri.graphicsExtent(results.features).expand(1.5)); 
    
}

0 Kudos
ToddMcNeil1
New Contributor
I've been trying for while to select/highlight the features returned from the querytask.  Based on the API samples, it would seem that it would be easy to do yet I've been having a hard time with it and am getting frustrated.  Based on the code I posted, how would I go about doing this?   I would like to zoom in to the extent of the features then highlight them.  Any help is appreciated.

Thanks,

Todd McNeil
Geographer

U.S. Department of Housing and Urban Development
Office of Policy Development and Research
0 Kudos
HemingZhu
Occasional Contributor III
I've been trying for while to select/highlight the features returned from the querytask.  Based on the API samples, it would seem that it would be easy to do yet I've been having a hard time with it and am getting frustrated.  Based on the code I posted, how would I go about doing this?   I would like to zoom in to the extent of the features then highlight them.  Any help is appreciated.

Thanks,

Todd McNeil
Geographer

U.S. Department of Housing and Urban Development
Office of Policy Development and Research


If you want to highlight the search result, you could query on a feature layer using featureLayer.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW). Once selectFeatures is fired up, in onSelectionComplete(features, selectionMethod), you could use map.extent =map.extent =esri.graphicsExtent(features).expand(1.5); if you set  featureLayer.setSelectionSymbol(highlightsymbol), then selected features will use highlightsymbol to display those features...
Hope it will help!
0 Kudos
ToddMcNeil1
New Contributor
hzhu,

For one thing, I am adding two Feature Layers to the map, state and county.  So I can read up on the querying and selecting through the samples.

What if I keep going in the same direction that I have been?  Is it still possible to do what I need or is it worth the time to use the Feature Layer methods and properties instead?

Thanks!

Todd
0 Kudos