Skip to content

Instantly share code, notes, and snippets.

@neilkennedy
Last active December 19, 2015 22:39
Show Gist options
  • Save neilkennedy/6029238 to your computer and use it in GitHub Desktop.
Save neilkennedy/6029238 to your computer and use it in GitHub Desktop.
The "description" function returns a latitude & longitude value a certain amount of kilometers away from another latitude longitude point. This code was adapted from some Java code I found at http://forum.processing.org/topic/calculating-lat-and-long-from-bearing-and-distance The "circle" function uses the "distance" function to return an array …
function circle (curLon, curLat, distanceKm) {
var i = 0;
var circle = [];
while (i < 360) {
circle.push(this.destination(curLon,curLat, i, distanceKm));
i += 20;
}
return circle;
}
function destination (curLon, curLat, bearing, distanceKm) {
//http://forum.processing.org/topic/calculating-lat-and-long-from-bearing-and-distance
var Eradius = 6371; // mean radius of the earth
var DestLat = Math.asin(Math.sin(radians(curLat)) * Math.cos(distanceKm / Eradius) + Math.cos(radians(curLat)) * Math.sin(distanceKm / Eradius) * Math.cos(radians(bearing)));
var DestLon = radians(curLon) + Math.atan2(Math.sin(radians(bearing)) * Math.sin(distanceKm / Eradius) * Math.cos(radians(curLat)), Math.cos(distanceKm / Eradius) - Math.sin(radians(curLat)) * Math.sin(DestLat));
DestLon = (DestLon + 3 * Math.PI) % (2 * Math.PI) - Math.PI; // normalise to -180..+180º
var lon = degrees(DestLon);
var lat = degrees(DestLat);
//console.log("starting at a Longitude of %f and a Latitude of %f ", curLon, curLat);
//console.log("if we travel %f km on a bearing of %f degrees ", distanceKm, bearing);
//console.log("we end up at Longitude of %f and a Latitude of %f ", lon, lat);
return { longitude: lon, latitude: lat };
}
//http://cwestblog.com/2012/11/12/javascript-degree-and-radian-conversion/
function radians(degrees) {
return degrees * Math.PI / 180;
}
function degrees(radians) {
return radians * 180 / Math.PI;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment