Get length of each path of Polyline

3814
3
07-21-2016 07:18 PM
JohnPreston
Occasional Contributor

I'm using geometryService.length to get length of Polyline, I would like to get and label each of segments (paths) of the polyline. Does geometryService return the lengths of each of the paths in results?

0 Kudos
3 Replies
FC_Basson
MVP Regular Contributor

Based on the REST API documention [Lengths (Operation) ] it should return the length of each path.

JohnPreston
Occasional Contributor

Can you point me to of example to get the values? I'm trying looking at documentation here GeometryService (legacy) | API Reference | ArcGIS API for JavaScript 3.17 but can figure out how to loop through lengths. I've tried

geometryService.on("lengths-complete", function (result) {   

dojo.forEach(result.result.lengths, function (length) {         

alert(length);   

});

}); but it only returns 1 length.

0 Kudos
FC_Basson
MVP Regular Contributor

I tested and it seems that the Lengths operation  returns the full length for a multipath polyline geometry.  So you would still need to iterate the polyline paths for getting the individual path length.

Try this to return the polyline path lengths:

var polyLine = new Polyline(...);  // the polyline you want path lenghts for
// iterate polyline paths
for (p in polyLine.paths){
  var tmpline = new Polyline({
    "paths": [polyLine.paths
],
    "spatialReference": polyLine.spatialReference
  });
  // get length for each path from geometry service
  getLengths(tmpline);
}

function getLengths(geom){
  var lengthParams = new LengthsParameters();
  lengthParams.polylines = [geom];
  lengthParams.lengthUnit = geomService.UNIT_METER;
  lengthParams.geodesic = true;
  geomService.lengths(lengthParams, function(result){
    console.log(result.lengths[0]);
  });
}

You might need to do some organising of the results to make sure the order is right.