Ganesh,
What you are looking for is snapping. SnappingManager provides exact the same as you described.
https://developers.arcgis.com/javascript/jsapi/snappingmanager-amd.html#getsnappingpoint
HI GuysI found the answer finding all the points in a polyline and getting their distance and here is the code below.If anyone need this, can you use them to suit their needs.// ********************************************************************
// Gets the distance from user's location for my nearest feature types.
//
// Parameters:
// -----------
// featureGeometryType - the type of feature.
// feature - the feature itself.
// location - the location parameter for my nearest service.
// ********************************************************************
function GetDistance(featureGeometryType, feature, location)
{
try
{
var x = GetXFromLocationParameter(location);
var y = GetYFromLocationParameter(location);
var startPoint = new esri.geometry.Point(x, y, _map.spatialReference);
// alert(startPoint.x + "," + startPoint.y);
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 };
// alert(feature.attributes.ROUTE_NAME + "<br/>" + feature.geometry.paths.length.toString() + "," + feature.geometry.paths.toString());
//For each path...
var pointArray =[];
var nearestPoint = null;
var nearestDistance = 1000;
for ( var path = 0; path < feature.geometry.paths.length; path ++ ) {
//For each point in the path...
for ( var pt = 0; pt < feature.geometry.paths[path].length; pt++ ) {
//Do something with each point in here...
//X coordinate: geo.paths[path][pt][0]
//Y coordinate: geo.paths[path][pt][1]
// alert(feature.geometry.paths[path][pt][0] + "," + feature.geometry.paths[path][pt][1]);
var linex = feature.geometry.paths[path][pt][0];
var liney = feature.geometry.paths[path][pt][1];
var linedistance = GetLinePoint2PointDistance(startPoint, linex, liney);
if (linedistance < nearestDistance)
{
nearestPoint = new esri.geometry.Point(linex, liney, _map.spatialReference);
nearestDistance = linedistance;
}
// alert(linedistance);
// pointArray.push(linedistance);
// alert(nearestPoint.x + ", " + nearestPoint.y + ", " + nearestDistance);
// pointArray.sort(function(a,b) {return a-b});
// pointArray.push(feature.geometry.paths[path][pt][0],feature.geometry.paths[path][pt][1]);
console.log(pointArray);
}
//alert(nearestPoint.x + ", " + nearestPoint.y + ", " + nearestDistance);
}
// alert( feature.attributes.ROUTE_NAME + "," + nearestPoint.x + ", " + nearestPoint.y + ", " + nearestDistance);
// alert(feature.attributes.ROUTE_NAME + ", " + pointArray[0]);
var myLine = new esri.geometry.Polyline(polylineJson);
var lineExtent = myLine.getExtent();
// endPoint = lineExtent.getCenter();
endPoint = nearestPoint;
break;
}
return GetPoint2PointDistance(startPoint, endPoint);
}
catch(ex)
{
C_HandleError(ex, "method GetDistance of search.js");
}
}
// Returns the distance from the points in the polyline to the address point.
function GetLinePoint2PointDistance(startPoint, linex, liney)
{
try
{
var distanceInMetres;
var distanceInMiles;
distanceInMetres = Math.pow((linex - startPoint.x), 2) + Math.pow((liney - startPoint.y), 2);
distanceInMetres = Math.sqrt(distanceInMetres);
// Convert to miles.
distanceInMiles = distanceInMetres * 0.00062137119;
distanceInMiles = FormatNearestNumber(distanceInMiles);
return distanceInMiles;
}
catch(ex)
{
C_HandleError(ex, "method GetLinePoint2PointDistance 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)
{
C_HandleError(ex, "method GetPoint2PointDistance of search.js");
}
}