This formula for calculating the ‘loxodromic midpoint’, the point half-way along a rhumb line between two points, is due to Robert Hill and Clive Tooth1 (thx Axel!).
| Formula: | latm = (lat1+lat2)/2 | |
| f1 = tan(π/4+lat1/2) | ||
| f2 = tan(π/4+lat2/2) | ||
| fm = tan(π/4+latm/2) | ||
| lonm = [ (lon2−lon1).ln(fm) + lon1.ln(f2) − lon2.ln(f1) ] / ln(f2/f1) | ||
| where ln is natural log | ||
| JavaScript: |
var lat3 =(lat1+lat2)/2;var f1 =Math.tan(Math.PI/4+ lat1/2);var f2 =Math.tan(Math.PI/4+ lat2/2);var f3 =Math.tan(Math.PI/4+ lat3/2);var lon3 =((lon2-lon1)*Math.log(f3)+ lon1*Math.log(f2)- lon2*Math.log(f1))/Math.log(f2/f1); Using the scripts in web pagesUsing these scripts in web pages would be something like the following:
|
<script >/* Latitude/Longitude formulae */</script> <script >/* Geodesy representation conversions */</script> ... <form> Lat1: <input type="text" name="lat1" id="lat1"> Lon1: <input type="text" name="lon1" id="lon1"> Lat2: <input type="text" name="lat2" id="lat2"> Lon2: <input type="text" name="lon2" id="lon2"> <button onClick="var p1 = new LatLon(Geo.parseDMS(f.lat1.value), Geo.parseDMS(f.lon1.value)); var p2 = new LatLon(Geo.parseDMS(f.lat2.value), Geo.parseDMS(f.lon2.value)); alert(p1.distanceTo(p2));">Calculate distance</button> </form>
If you use jQuery, the code can be separated from the HTML:
<script ></script>
<script >/* Latitude/Longitude formulae */</script>
<script >/* Geodesy representation conversions */</script>
<script>
$(document).ready(function() {});
});
</script>
...
<form>
Lat1: <input type="text" name="lat1" id="lat1"> Lon1: <input type="text" name="lon1" id="lon1">
Lat2: <input type="text" name="lat2" id="lat2"> Lon2: <input type="text" name="lon2" id="lon2">
<button id="calc-dist">Calculate distance</button>
<output id="result-distance"></output>
</form>
--------------------------------------------------------------------------------
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2011 */
/* - www.movable-type.co.uk/scripts/latlong.html */
/* */
/* Sample usage: */
/* var p1 = new LatLon(51.5136, -0.0983); */
/* var p2 = new LatLon(51.4778, -0.0015); */
/* var dist = p1.distanceTo(p2); // in km */
/* var brng = p1.bearingTo(p2); // in degrees clockwise from north */
/* ... etc */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Note that minimal error checking is performed in this example code! */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Creates a point on the earth's surface at the supplied latitude / longitude
*
* @constructor
* @param {} lat: latitude in numeric degrees
* @param {} lon: longitude in numeric degrees
* @param {} [rad=6371]: radius of earth if different value is required from standard 6,371km
*/
function LatLon(lat, lon, rad) {}
/**
* Returns the distance from this point to the supplied point, in km
* (using Haversine formula)
*
* from: Haversine formula - R. W. Sinnott, "Virtues of the Haversine",
* Sky and Telescope, vol 68, no 2, 1984
*
* @param {} point: Latitude/longitude of destination point
* @param {} [precision=4]: no of significant digits to use for returned value
* @returns {} Distance in km between this point and destination point
*/
LatLon.prototype.distanceTo = function(point, precision) {}
/**
* Returns the (initial) bearing from this point to the supplied point, in degrees
* see avform.htm#Crs
*
* @param {} point: Latitude/longitude of destination point
* @returns {} Initial bearing in degrees from North
*/
LatLon.prototype.bearingTo = function(point) {}
/**
* Returns final bearing arriving at supplied destination point from this point; the final bearing
* will differ from the initial bearing by varying degrees according to distance and latitude
*
* @param {} point: Latitude/longitude of destination point
* @returns {} Final bearing in degrees from North
*/
LatLon.prototype.finalBearingTo = function(point) {}
/**
* Returns the midpoint between this point and the supplied point.
* see library/drmath/view/51822.html for derivation
*
* @param {} point: Latitude/longitude of destination point
* @returns {} Midpoint between this point and the supplied point
*/
LatLon.prototype.midpointTo = function(point) {}
/**
* Returns the destination point from this point having travelled the given distance (in km) on the
* given initial bearing (bearing may vary before destination is reached)
*
* see avform.htm#LL
*
* @param {} brng: Initial bearing in degrees
* @param {} dist: Distance in km
* @returns {} Destination point
*/
LatLon.prototype.destinationPoint = function(brng, dist) {}
/**
* Returns the point of intersection of two paths defined by point and bearing
*
* see avform.htm#Intersection
*
* @param {} p1: First point
* @param {} brng1: Initial bearing from first point
* @param {} p2: Second point
* @param {} brng2: Initial bearing from second point
* @returns {} Destination point (null if no unique intersection defined)
*/
LatLon.intersection = function(p1, brng1, p2, brng2) {} else {}
alpha1 = (brng13 - brng12 + Math.PI) % (2*Math.PI) - Math.PI; // angle 2-1-3
alpha2 = (brng21 - brng23 + Math.PI) % (2*Math.PI) - Math.PI; // angle 1-2-3
if (Math.sin(alpha1)==0 && Math.sin(alpha2)==0) return null; // infinite intersections
if (Math.sin(alpha1)*Math.sin(alpha2) < 0) return null; // ambiguous intersection
//alpha1 = Math.abs(alpha1);
//alpha2 = Math.abs(alpha2);
// ... Ed Williams takes abs of alpha1/alpha2, but seems to break calculation?
alpha3 = Math.acos( -Math.cos(alpha1)*Math.cos(alpha2) +
Math.sin(alpha1)*Math.sin(alpha2)*Math.cos(dist12) );
dist13 = Math.atan2( Math.sin(dist12)*Math.sin(alpha1)*Math.sin(alpha2),
Math.cos(alpha2)+Math.cos(alpha1)*Math.cos(alpha3) )
lat3 = Math.asin( Math.sin(lat1)*Math.cos(dist13) +
Math.cos(lat1)*Math.sin(dist13)*Math.cos(brng13) );
dLon13 = Math.atan2( Math.sin(brng13)*Math.sin(dist13)*Math.cos(lat1),
Math.cos(dist13)-Math.sin(lat1)*Math.sin(lat3) );
lon3 = lon1+dLon13;
lon3 = (lon3+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180º
return new LatLon(lat3.toDeg(), lon3.toDeg());
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Returns the distance from this point to the supplied point, in km, travelling along a rhumb line
*
* see avform.htm#Rhumb
*
* @param {} point: Latitude/longitude of destination point
* @returns {} Distance in km between this point and destination point
*/
LatLon.prototype.rhumbDistanceTo = function(point) {}
/**
* Returns the bearing from this point to the supplied point along a rhumb line, in degrees
*
* @param {} point: Latitude/longitude of destination point
* @returns {} Bearing in degrees from North
*/
LatLon.prototype.rhumbBearingTo = function(point) {}
/**
* Returns the destination point from this point having travelled the given distance (in km) on the
* given bearing along a rhumb line
*
* @param {} brng: Bearing in degrees from North
* @param {} dist: Distance in km
* @returns {} Destination point
*/
LatLon.prototype.rhumbDestinationPoint = function(brng, dist) {}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Returns the latitude of this point; signed numeric degrees if no format, otherwise format & dp
* as per Geo.toLat()
*
* @param {} [format]: Return value as 'd', 'dm', 'dms'
* @param {} [dp=0|2|4]: No of decimal places to display
* @returns {} Numeric degrees if no format specified, otherwise deg/min/sec
*
* @requires Geo
*/
LatLon.prototype.lat = function(format, dp) {}
/**
* Returns the longitude of this point; signed numeric degrees if no format, otherwise format & dp
* as per Geo.toLon()
*
* @param {} [format]: Return value as 'd', 'dm', 'dms'
* @param {} [dp=0|2|4]: No of decimal places to display
* @returns {} Numeric degrees if no format specified, otherwise deg/min/sec
*
* @requires Geo
*/
LatLon.prototype.lon = function(format, dp) {}
/**
* Returns a string representation of this point; format and dp as per lat()/lon()
*
* @param {} [format]: Return value as 'd', 'dm', 'dms'
* @param {} [dp=0|2|4]: No of decimal places to display
* @returns {} Comma-separated latitude/longitude
*
* @requires Geo
*/
LatLon.prototype.toString = function(format, dp) {}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
// ---- extend Number object with methods for converting degrees/radians
/** Converts numeric degrees to radians */
if (typeof(Number.prototype.toRad) === "undefined") {}
}
/** Converts radians to numeric (signed) degrees */
if (typeof(Number.prototype.toDeg) === "undefined") {}
}
/**
* Formats the significant digits of a number, using only fixed-point notation (no exponential)
*
* @param {} precision: Number of significant digits to appear in the returned string
* @returns {} A string representation of number which contains precision significant digits
*/
if (typeof(Number.prototype.toPrecisionFixed) === "undefined") {}
var scale = Math.ceil(Math.log(numb)*Math.LOG10E); // no of digits before decimal
var n = String(Math.round(numb * Math.pow(10, precision-scale)));
if (scale > 0) {} else {}
return sign + n;
}
}
/** Trims whitespace from string (q.v. blog.stevenlevithan.com/archives/faster-trim-javascript) */
if (typeof(String.prototype.trim) === "undefined") {}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */