Created
September 5, 2009 04:40
-
-
Save voxxit/181290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Calculate destination point given start point, initial bearing (deg) and distance (km) | |
LatLon.prototype.destPoint = function(brng, d) { | |
var R = 6371; // Earth's mean radius in km | |
var lat1 = this.lat.toRad(); | |
var lon1 = this.lon.toRad(); | |
var brng = brng.toRad(); | |
var lat2 = Math.asin(Math.sin(lat1) * Math.cos(d / R) + | |
Math.cos(lat1) * Math.sin(d / R) * Math.cos(brng)); | |
var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(d / R) * Math.cos(lat1), | |
Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2)); | |
// Normalise to -180..+180 | |
lon2 = (lon2 + Math.PI) % (2 * Math.PI) - Math.PI; | |
if (isNaN(lat2) || isNaN(lon2)){ | |
return null | |
} else { | |
return new LatLon(lat2.toDeg(), lon2.toDeg()); | |
} | |
} | |
// Convert degrees to radians | |
Number.prototype.toRad = function(){ | |
return this * Math.PI / 180; | |
} | |
// Convert radians to degrees (signed) | |
Number.prototype.toDeg = function(){ | |
return this * 180 / Math.PI; | |
} | |
// Convert radians to degrees (as bearing: 0..360) | |
Number.prototype.toBrng = function(){ | |
return (this.toDeg()+360) % 360; | |
} | |
// Construct a LatLon object: arguments in numeric degrees | |
// NOTE: All LatLong methods expect & return numeric degrees (for lat/long & for bearings) | |
function LatLon(lat, lon){ | |
this.lat = lat; | |
this.lon = lon; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment