There are several different ways to calculate the distance between two coordinates, P_{1}(r, θ, Φ) and P_{2}(r, θ, Φ). The first method consists of converting the points to Cartesian coordinates, and then calculating the cord length (d) between the points (straight line distance through the Earth). From there we can calculate the central angle between the two points (δ). Once we have the central angle between the two points we can make a simple calculation to figure out the arc length (L) between the two points.

Figure 1 Arc Length calculation

To calculate the cord length between P_{1} and P_{2} we can use Pythagorean Theorem as follows:

Now that we have the cord length we can calculate the central angle between the two points. Using simple trigonometry we can calculate from *r* and as follows:

We want to be in radians. If we calculate using javascript the angle will be in radians. If we calculate using a calculator we will have to convert the angle from degrees to radians. We can then calculate the arc length using the following formula:

The second method for calculating the distance between two points is the Haversine method. The basic principal is similar to the last method of calculating the distance between two points except that it does not solve for x, y, and z, but uses the formulas of the components. The cord length between the two points can be calculated by using Pythagorean Theorem against the displacement in the x, y, and z axis.

By using the following trigonometric identities we can derive a simpler formula for the cord length between two points.

We can then use the atan2 function to derive the central angle between the two points:

We can then calculate the distance between the two points using the following formula:

Both of these methods produce fairly accurate results. If more precision is required the Vincenty formula can be used which calculates the distance between two points on an ellipsoid. The basic principals are similar to the other two formulas except that the radius varies between the two points.

**Application**

For the function that calculates the arc length between two points it will take in to VELatLong objects and return a number. This function has to convert the VELatLongs into Cartesian coordinates. When we use the convertSphericalToCartesian function the returned value is a Cartesian coordinate object with X, Y, and Z values.

var earthRadius = 6367; //radius in km function arcLength(latlong1, latlong2){ var point1 = convertSphericalToCartesian(latlong1); var point2 = convertSphericalToCartesian(latlong2); var cordLength = Math.sqrt(Math.pow(point1.X-point2.X,2)+Math.pow(point1.Y-point2.Y,2)+Math.pow(point1.Z-point2.Z,2)); var centralAngle = 2*Math.asin(cordLength/2/earthRadius); return earthRadius*centralAngle; }

Listing 1 Arc Length function

The Haversine function will take in to VELatLong objects and return a number. In this function we will need to convert the latitude and longitude values from degrees to radians using the DegtoRad function.

function haversineDistance(latlong1,latlong2){ var lat1 = DegtoRad(latlong1.Latitude); var lon1 = DegtoRad(latlong1.Longitude); var lat2 = DegtoRad(latlong2.Latitude); var lon2 = DegtoRad(latlong2.Longitude); var dLat = lat2-lat1; var dLon = lon2-lon1; var cordLength = Math.pow(Math.sin(dLat/2),2)+Math.cos(lat1)*Math.cos(lat2)*Math.pow(Math.sin(dLon/2),2); var centralAngle = 2 * Math.atan2(Math.sqrt(cordLength), Math.sqrt(1-cordLength)); return earthRadius * centralAngle; }

Listing 2 Haversine function

The following post has additional information on the RadToDeg and DegToRad methods: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!257.entry

The following post has additional information on the convertSphericalToCartesian method: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!280.entry