how to get the mid point of the polyline in arcgis javascript api

582
1
09-13-2019 06:53 AM
Satyanarayana
New Contributor

How to get the mid point of the Poly line in javascript 

so i can place a lable for the drawn line

0 Kudos
1 Reply
ScottDavis3
New Contributor II

Try the code below. The get_length is another function that just uses the geometryEngine length function. This works for a single part polyline.

function midPointPolyline(line) {
var path = line.paths[0];
var seglen = get_length(line, "feet");
var midLen = seglen / 2;
var currentDistance = 0;
var beforeIndex = 0;
var startPoint = line.getPoint(0, 0);
for (i = 1; i < path.length - 1; i++) {
var nextPoint = line.getPoint(0, i);
var d = geometryEngine.distance(startPoint, nextPoint, "feet");
if (currentDistance + d < midLen) {
currentDistance += d;
startPoint = nextPoint;
} else {
beforeIndex = (i === 1) ? 0 : i;
break;
}
}
startPoint = line.getPoint(0, beforeIndex);
var endPoint = line.getPoint(0, beforeIndex + 1);
var x = (startPoint.x + endPoint.x) / 2;
var y = (startPoint.y + endPoint.y) / 2;
var midpt = new esri.geometry.Point({ x, y, spatialReference: { wkid: line.spatialReference.wkid } });
return midpnt;
}

0 Kudos