Skip to content

Instantly share code, notes, and snippets.

@geraldstanje
Last active April 14, 2017 05:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraldstanje/d2709726f7d0c3b749d8a7541d66093e to your computer and use it in GitHub Desktop.
Save geraldstanje/d2709726f7d0c3b749d8a7541d66093e to your computer and use it in GitHub Desktop.
generate random location (lat, long)
function getRandomCoordinates(radius, uniform) {
// Generate two random numbers
var a = Math.random(),
b = Math.random();
// Flip for more uniformity.
if (uniform) {
if (b < a) {
var c = b;
b = a;
a = c;
}
}
// It's all triangles.
return [
b * radius * Math.cos(2 * Math.PI * a / b),
b * radius * Math.sin(2 * Math.PI * a / b)
];
}
function getRandomLocation(latitude, longitude, radiusInMeters) {
var randomCoordinates = getRandomCoordinates(radiusInMeters, true);
// Earths radius in meters via WGS 84 model.
var earth = 6378137;
// Offsets in meters.
var northOffset = randomCoordinates[0],
eastOffset = randomCoordinates[1];
// Offset coordinates in radians.
var offsetLatitude = northOffset / earth,
offsetLongitude = eastOffset / (earth * Math.cos(Math.PI * (latitude / 180)));
// Offset position in decimal degrees.
return {
latitude: latitude + (offsetLatitude * (180 / Math.PI)),
longitude: longitude + (offsetLongitude * (180 / Math.PI))
}
}
// var loc = getRandomLocation(44.676998138,-63.612499237, 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment