Why is this useful? Let’s say you have a map of cat sightings, and you want a unique list of cat breeds. One way you could do it is to query for the entire list, then sort and cut out all the repeating values. That would look something like this:
The problem here two-fold. First, if the list is long, the array sorting and splicing will take a significant amount of time, especially on mobile browsers. Secondly, ArcGIS Server has a limit on the number of results it returns (default 1000-2000). If you have 10,000+ cat sightings, you’re not going to get everything on the list. You may miss the sighting of a Sphynx, a Ukrainian Lekvoy, or even a Grumpy.
Strangely enough, this works as long as you’re just querying using where clauses and text. Executing a query on at QueryTask breaks if you try to do the following.
- Get a count (queryForCount())
- Query for ObjectIDs
- Send a Query with a list of ObjectIDs
There may be more, but that’s as far as I checked. Another method, as mentioned here, uses the esriRequest.setRequestPreCallback() method to dig into requests (including queries), and add the returnDistinctValue = true parameter to the request. Here is a more robust way to add the value to any query request. Warning: this does contain regex, which may warp your brain if you let it.
I hope this helps somebody out there.