I need something as simple as
\"Subject 1.02: How do I find the distance from a point to a line?\"
But that works with Lon/Lat.
You have a line through earth-surface points A and B and a point C which you want to compute distance from.
You can compute the area of triangle ABC, divide it by distance between A and B and then multiply by 2.
function computeDistanceToLine(p, line) {
var length = computeDistanceBetween(line.startPoint, line.endPoint);
var triangleArea = computeTriangleArea(p, line.startPoint, line.endPoint);
return 2 * triangleArea / length;
}
The algorithm for computing distance between two points is well-known. There are tons of implementations. One of them (as noticed in previous answers) can be found there http://www.movable-type.co.uk/scripts/latlong.html
For computing triangle area you can use some algorithm dependent on line lengths. For example
function computeTriangleArea(p0, p1, p2) {
var r = 6378137;
var d0 = computeDistanceBetween(p0, p1);
var d1 = computeDistanceBetween(p1, p2);
var d2 = computeDistanceBetween(p2, p0);
var halfPerimeter = (d0 + d1 + d2) * 0.5;
var t = Math.tan(halfPerimeter) *
Math.tan((halfPerimeter - d0) * 0.5) *
Math.tan((halfPerimeter - d1) * 0.5) *
Math.tan((halfPerimeter - d2) * 0.5);
return 4 * Math.atan(Math.sqrt(Math.abs(t))) * r * r;
}