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");
}
}
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"); } }
We don't have a method that returns all points from a polyline. You'll have to loop through polyline.paths and look at points from there.
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
// ********************************************************************
// 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");
}
}