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);
}
});