Last active
April 14, 2017 05:39
-
-
Save geraldstanje/d2709726f7d0c3b749d8a7541d66093e to your computer and use it in GitHub Desktop.
generate random location (lat, long)
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
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