ganeshssac

How to get all points or vertex of polyline using arcgis javascript api

Discussion created by ganeshssac on Mar 24, 2014
Latest reply on May 12, 2014 by ganeshssac
Hi Guys,
I wanted to find the nearest point in the polyline from an input location and calculate its distance. At the moment, the application queries based on centroid of a polyline. In my case, I wanted to find all the points in the polyline and then push those points in an array and find the nearest point in the polyline from an input location.

Here is my current code below: All I wanted to do is change the code when esri Geometry is polyline and try find all points in the feature. Can anybody help me on this please.

function GetDistance(featureGeometryType, feature, location)
{
    try
    {
        var x = GetXFromLocationParameter(location);
        var y = GetYFromLocationParameter(location);
        var startPoint = new esri.geometry.Point(x, y, _map.spatialReference);
        var endPoint;

        switch (featureGeometryType)
        {
            case "esriGeometryPoint":
                endPoint = new esri.geometry.Point(feature.geometry.x, feature.geometry.y, _map.spatialReference);
                break;

            case "esriGeometryPolygon":
                var polygonJson = { "rings": feature.geometry.rings, "spatialReference": _map.spatialReference };
                var myPolygon = new esri.geometry.Polygon(polygonJson);
                var polyExtent = myPolygon.getExtent();
                endPoint = polyExtent.getCenter();
                break;

            case "esriGeometryPolyline":
                var polylineJson = { "paths": feature.geometry.paths, "spatialReference": _map.spatialReference };
                var myLine = new esri.geometry.Polyline(polylineJson);
                var lineExtent = myLine.getExtent();
                endPoint = lineExtent.getCenter();

                break;
        }

        return GetPoint2PointDistance(startPoint, endPoint);
    }
    catch(ex)
    {
        HandleError(ex, "method GetDistance of search.js");
    }
}

// Returns the distance from passed in point to the address point.
function GetPoint2PointDistance(startPoint, endPoint)
{
    try
    {
        var distanceInMetres;
        var distanceInMiles;

        distanceInMetres = Math.pow((endPoint.x - startPoint.x), 2) + Math.pow((endPoint.y - startPoint.y), 2);
        distanceInMetres = Math.sqrt(distanceInMetres);

        // Convert to miles.
        distanceInMiles = distanceInMetres * 0.00062137119;
        distanceInMiles = FormatNearestNumber(distanceInMiles);
        return distanceInMiles;
    }
    catch(ex)
    {
        HandleError(ex, "method GetPoint2PointDistance of search.js");
    }
}

Outcomes