# Calculating distance between two points with esri.geometry in ArcGIS API for JavaScript?

The function getLength(point1, point2) should calculate distances between two points.

When I am trying to calculate distance between points having 100 meters between them

``````p1=new esri.geometry.Point(3997030.6690969253, 7444299.320646087, new esri.SpatialReference({ wkid: 102113 }));
Object
p2=new esri.geometry.Point(3996951.455397143, 7444142.154020177, new esri.SpatialReference({ wkid: 102113 }));
Object
esri.geometry.getLength(p1, p2)
176.00045037719127
``````

I am getting 176 which is wrong. Projection is Web Mercator (WKID 102113).

@Krystian found that `getLength` uses just Pythagoras theorem to calculate length. So it gives length in projection meters. How to obtain distance in real meters?

• try use geoprocessor tool to check your distance (esri.tasks.GeometryService.lengths() ), maybe you want have geodetic distance between those points, and getLength method is counting straight length between points (it's just my blind shot) – Krystian Jan 31 '13 at 12:47
• It's 100 meters. Difference between two methods should not be 76 meters. This is most probably because `getLength` does not understand projection. Or may be I don't understand something. – Suzan Cioc Jan 31 '13 at 12:59
• – mkennedy Jan 31 '13 at 18:01

## 2 Answers

Web Mercator Auxiliary Sphere is WKID 102100

http://resources.arcgis.com/en/help/arcgisonline-content/index.html#//011q00000002000000

``````var polyline = {
"paths":[[[3997030.6690969253, 7444299.320646087], [3996951.455397143, 7444142.154020177]]],
"spatialReference":{"wkid":102100}
};
console.log(JSON.stringify(polyline));
polyline = new esri.geometry.Polyline(polyline);
console.log(JSON.stringify(polyline));
polyline = esri.geometry.webMercatorToGeographic(polyline);
console.log(JSON.stringify(polyline));
console.log(esri.geometry.geodesicLengths([polyline], esri.Units.METERS));
//output 99.93917832865446
``````

6.1 cm short, good enough for govment work?

I just checked it.

`esri.geometry.getLength(p1, p2)` is counting distance like this:

`length = sqrt((p2_X - p1_X)^2 + (p2_Y - P1_Y)^2)`

so it is distance between two points in straight line. If you want to count distance between two points on geodetic line you should use geometry service as in this example

• Algorithm looks wrong. But it's problem is not that it takes straight line, but that it ignores projection. – Suzan Cioc Jan 31 '13 at 15:31
• You don't really need the geometry service. Although, it would be an interesting to test the difference between the javascript API and the geometry service. – awesomo Jan 31 '13 at 19:08