Last active
December 19, 2015 22:39
-
-
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 …
This file contains hidden or 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
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