Simply getting a count of the features meeting certain criteria is sometimes sufficient, but it’s often necessary to work with the features themselves. To do that, you use the queryFeatures() method. As with queryFeatureCount(), queryFeatures() returns a promise. The difference is that the queryFeatures() promise resolves to a FeatureSet object rather than a Number.
The example above uses the same Query where clause as the previous ones and displays the counties meeting the criterion as graphics. Note the following important points:
- A new empty GraphicsLayer object is created to hold the counties meeting the query criterion.
- The Query has its returnGeometry property set to true. This is necessary to be able to map the results.
- The queryFeatures() method is executed after the counties layer has finished loading (line 41).
- The displayResults() function is defined such that the FeatureSet returned by queryFeatures() is stored in a variable called results.
- Getting at the counties in the FeatureSet is done by reading its features property, which returns an array of Graphic objects.
- The new array of Graphic objects is added to the GraphicsLayer (in the resultsLayer variable).
So far, where and returnGeometry are the only Query properties we’ve looked at. However, there are several others that are important in certain contexts. Here is a brief description of just a few of these properties:
- num – the maximum number of features to return; often used together with the start property to implement paging of results
- orderByFields – an array of fields to use for sorting the results
- outFields – an array of fields to include in the results; limiting this array to only what you need can improve performance
There are actually a few more Query properties that I think are worth discussing, but I left them out of this list because they’re considered in greater depth in the next section on spatial queries.