Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.