Created
May 24, 2013 02:55
-
-
Save mkhatib/5641004 to your computer and use it in GitHub Desktop.
A Javascript utility function to generate number of random Geolocations around a center location and in a defined radius.
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
/** | |
* Generates number of random geolocation points given a center and a radius. | |
* @param {Object} center A JS object with lat and lng attributes. | |
* @param {number} radius Radius in meters. | |
* @param {number} count Number of points to generate. | |
* @return {array} Array of Objects with lat and lng attributes. | |
*/ | |
function generateRandomPoints(center, radius, count) { | |
var points = []; | |
for (var i=0; i<count; i++) { | |
points.push(generateRandomPoint(center, radius)); | |
} | |
return points; | |
} | |
/** | |
* Generates number of random geolocation points given a center and a radius. | |
* Reference URL: http://goo.gl/KWcPE. | |
* @param {Object} center A JS object with lat and lng attributes. | |
* @param {number} radius Radius in meters. | |
* @return {Object} The generated random points as JS object with lat and lng attributes. | |
*/ | |
function generateRandomPoint(center, radius) { | |
var x0 = center.lng; | |
var y0 = center.lat; | |
// Convert Radius from meters to degrees. | |
var rd = radius/111300; | |
var u = Math.random(); | |
var v = Math.random(); | |
var w = rd * Math.sqrt(u); | |
var t = 2 * Math.PI * v; | |
var x = w * Math.cos(t); | |
var y = w * Math.sin(t); | |
var xp = x/Math.cos(y0); | |
// Resulting point. | |
return {'lat': y+y0, 'lng': xp+x0}; | |
} | |
// Usage Example. | |
// Generates 100 points that is in a 1km radius from the given lat and lng point. | |
var randomGeoPoints = generateRandomPoints({'lat':24.23, 'lng':23.12}, 1000, 100); |
Thanks for this. Using this to generate dummy data to simulate Covid-19 tracking data.
Thanks!
Very helpful. Thanks for posting this super useful function!
Hello, this was critically helpful, man! This is exactly what I needed! I re-implemented it in Python and it works like charm! Thank you again!
Thanks for this elegant utility. It is of great help for me...
thanks man its working good
in the open source days, a Star is a 'thanks'.
Thanks for make our life easier
Thanks alot. <3
Very nice thank you very much!
Z UCV CV lxldj un redzvww ñedzd
El mié., 3 nov. 2021 22:58, Webb24h ***@***.***> escribió:
… ***@***.**** commented on this gist.
------------------------------
Very nice thank you very much!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/5641004#gistcomment-3950195>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFRI6JD72R3CKGMFDGNWCEDUKG47XANCNFSM4JAH65TQ>
.
Thank you
thanks!
This is exactly what I need, thank you for the sharing!
Thank you 😁
thanks!
Wonderful! Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AWESSSSSSOME! Works like a charm.