HOW TO LOOP WAITING FOR GEOMETRY SERVICE DISTANCE RETURN

791
2
01-26-2013 05:40 AM
WalterEralio
New Contributor III
Situation:
geometry 1: Point
geometry 2: segments (from an array of segments already returned by the call to the geometry service - buffer function)

I need to loop thru them getting the DISTANCE (again using the GeometryService distance function) just to compare and try to find the closest one... but the call will fire the onDistanceComplete(distance) whenever is done.. how I'm supposed to be waiting for this value efficiently?? just looping while the event is fired?? should I have to use a deferred methodology??

basically trying to make something like this works

for (segm=0; segm = segments.length; segm++)
{
geom1 = point
geom2 = segments[segm]

HOW I WAIT FORTHIS RETURN?
geometryService.distance(distParams, function(distance) {
currDistance= distance});

//Comparison currentDistance with minDistance
}
0 Kudos
2 Replies
JanelYang
New Contributor III
Walter,

The return of Geometry.distance is a dojo.Deferred object.
In the code you provided, anything in function(distance){} will always be executed after the return is got (if this request succeeds).
In other words, anything in the function will always wait for the return.

As for looping multiple geometryService.distance requests, try dojo.DeferredList . It is a multiple-input version of dojo.Deferred. Its input is a list of geometryService.distance, while the output is a list where each element is a return of each request.

You may also check out this post.

Jerome
0 Kudos
WalterEralio
New Contributor III
Thanks Jerome! will do!
0 Kudos