synchronous call to geometeryService.distance

3198
3
07-02-2015 03:04 PM
JohnPreston
Occasional Contributor

Hi I'm trying to sum the distances between points by loop through point in a path and calling the geometeryService.distance method then adding the result to a total variable, which is then used for another calculation. However, the code is not waiting for the "for loop" to complete before moving on to the next line (which surprised me). Am I doing something wrong in how I get the distance? Or how can wait the "for loop" to complete before moving on?

identifyTask.execute(identifyParams, function (idResults) {                      

if (idResults.length > 0) {                          

var polyl;                          

var mpdistance = 0;                          

for (var r = idResults.length - 1; r >= 0; r--) {                              

polyl = idResults.feature.geometry;                              

var feature = idResults.feature;                              

var geometryService = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");                              

var distParams = new DistanceParameters();                              

distParams.distanceUnit = GeometryService.UNIT_STATUTE_MILE;                              

distParams.geometry1 = evt.mapPoint;                             

distParams.geodesic = true;                              

dojo.forEach(polyl.paths, function (path, i) {                                  

var beginpoint = polyl.getPoint(0, 0);                                  

for (j = 0; j < path.length; j++) {                                      

mp = polyl.getPoint(i, j);                                      

distParams.geometry1 = beginpoint;                                      

distParams.geometry2 = mp                                      

distParams.geodesic = true;                                      

geometryService.distance(distParams, function (distance) {                                          

mpdistance += distance;                                     

});                                     

beginpoint = polyl.getPoint(i, j);                                  

}                              

});                         

}                          

map.infoWindow.setContent("Road Name:" + feature.attributes['RoadName'] + " MilePost:" + mpdistance + "");                           map.infoWindow.setTitle("Road:" + feature.attributes['RoadName']);                          

map.infoWindow.show(evt.mapPoint);                      

}                 

});

0 Kudos
3 Replies
PrevinWong1
Esri Contributor

Since you have multiple loops going on and actions in those loops calling deferred functions, you can add all the geometry calls in an object or array and wrap it in a "dojo/promise/all".  That way, the callback will not happen until the objects in the "all" have executed.  Another way is to have the GeometryService callback advance the loop, so that it becomes kinda synchronous.

Also, you can look into using geometryEngine instead of GeometryService to do your distance.

JohnPreston
Occasional Contributor

Thank you Previn,

I've been searching for an example, could you direct me to an example that uses nested "for loops?"

0 Kudos
JoshHevenor
Occasional Contributor II