featureLayer.queryFeatures order by closest first?

2274
2
Jump to solution
11-09-2018 03:08 AM
ShakilChoudhury
New Contributor III

I have a function that queries features that appear within a buffer around a given point on a feature layer.

function queryLayer(layer, buffer, point){
  var query = layer.createQuery();
  query.geometry = buffer;
  query.spatialRelationship = "intersects";
  layer.queryFeatures(query).then(function(results){
    console.log(results.features);
  });
}
‍‍‍‍‍‍‍‍

I know that 

query.orderByFields = "..."

exists but I'm not sure how to go about using it. Is there a good way I can use the point variable to say "order by closest to this point"?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Shakil,

   There is no spatial orderBy capabilities in the API. So you will have to do it manually in the returned results. To do this you need your buffer point of origin (center point) and then to loop through the results and do a geometryEngine.distance evaluation keeping an array of the distance results that sort the array on.

https://developers.arcgis.com/javascript/3/jsapi/esri.geometry.geometryengine-amd.html#distance 

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

Shakil,

   There is no spatial orderBy capabilities in the API. So you will have to do it manually in the returned results. To do this you need your buffer point of origin (center point) and then to loop through the results and do a geometryEngine.distance evaluation keeping an array of the distance results that sort the array on.

https://developers.arcgis.com/javascript/3/jsapi/esri.geometry.geometryengine-amd.html#distance 

RobertScheitlin__GISP
MVP Emeritus

Here is an old sample of how this could be done using a much older API version:

https://community.esri.com/thread/107086