-
-
Save omurbekjk/ae030379ee098d468896ae0029c003b9 to your computer and use it in GitHub Desktop.
A Typescript utility function to generate number of random Geolocations around a center location and in a defined radius.
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
export class GeoUtil { | |
/** | |
* @param center = {lat, lng} | |
* @param radius = 1000 | |
* @param count = 10 | |
*/ | |
static generateRandomPoints(center, radius = 1000, count = 10) { | |
const points = []; | |
for (let i = 0; i < count; i++) { | |
points.push(this.generateRandomPoint(center, radius)); | |
} | |
return points; | |
} | |
static generateRandomPoint(center, radius) { | |
const x0 = center.lng; | |
const y0 = center.lat; | |
// Convert Radius from meters to degrees. | |
const rd = radius / 111300; | |
const u = Math.random(); | |
const v = Math.random(); | |
const w = rd * Math.sqrt(u); | |
const t = 2 * Math.PI * v; | |
const x = w * Math.cos(t); | |
const y = w * Math.sin(t); | |
const xp = x / Math.cos(y0); | |
// Resulting point. | |
return {lat: y + y0, lng: xp + x0}; | |
} | |
} | |
// Usage | |
GeoUtil.generateRandomPoints(this.currentLatLng, 10000, 15); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment