Iterating Geometry Server Distance calculations on a Result Set

1021
2
08-02-2011 02:48 PM
JeffKapellas
New Contributor III
I've searched through the forums and a bunch of dojo sites on Google, but haven't been able to find an answer for a problem that has stumped me for the past couple of days. So, I thought I'd just go ahead and ask straight out.

I am trying to write a function that calculates the distances between a point and each feature that falls within a buffer around that point. After performing the spatial query, the result set from the query is passed to the function that then returns an array of objects containing selected attributes from the features and the distance calculation. Here's a code snippet.

                
var distanceParams = new esri.tasks.DistanceParameters();
distanceParams.distanceUnit = esri.tasks.GeometryService.UNIT_STATUTE_MILE;

distanceParams.geometry1 = myMap.geocodePoint; //the point being buffered 

var myAttributeArray = dojo.map(bufferQueryResultSet.features, function(res){
   
 var attr = {};
 attr.ID = res.attributes.ID;
 attr.NAME = res.attributes.NAME;
 distanceParams.geometry2 = res.geometry;
 myMap.geometryServer.distance(distanceParams, function(d){
      attr.DISTANCE = d;
 }).then(function(){return attr;});   
});


The function as written above returns myAttributeArray as an array of undefined objects. If I rewrite the last few lines as:

 myMap.geometryServer.distance(distanceParams, function(d){
      attr.DISTANCE = d;
 });
        return attr;


...then myAttributeArray comprises objects with only ID and NAME defined.

I also tried:
 attr.DISTANCE = myMap.geometryServer.distance(distanceParams, function(d){
      return d;
 });
        return attr;


...and that gave me an array of objects with ID and NAME defined and DISTANCE defined as a dojo.Deferred object.

I know that there must be something with dojo.Deferred that I'm overlooking, but I'm not sure what. Any assistance would be greatly appreciated.

Thanks!

Jeff Kapellas
California State Water Resources Control Board
2 Replies
derekswingley1
Frequent Contributor
I think there are two options:
1. Use a dojo.DeferredList to manage the deferreds returned from geometrySerivce.distance. That way, once all of your deferreds from the geometry service are resolved, you can access the results and go on building your array of objects. As is, if you access the myAttributeArray array immediately after dojo.map() runs, there could be resolved and unfulfilled promises. If you use a deferredList to manage everything, you are guaranteeing that your results will be there.

2. Do it all client side. The API includes client side functions to calculate distance between points. Check out esri.geometry.getLength() or, if you want geodesic distances, create a polyline out of your two points and pass it to esri.geometry.geodesicsLength().
0 Kudos
JeffKapellas
New Contributor III
Derek:

Thanks for the reply. I'll take a look at the DeferredList option. The esri.geometry.getLength() option only seems to work for point-to-point, and I need to do point-to-line and point-to-poly calculations.

Jeff
0 Kudos