Skip to content

Instantly share code, notes, and snippets.

@git-ashish
Forked from michiel/lat_lng_jitter.js
Created May 30, 2019 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save git-ashish/9c1d42f640659a558b2136faeafb6361 to your computer and use it in GitHub Desktop.
Save git-ashish/9c1d42f640659a558b2136faeafb6361 to your computer and use it in GitHub Desktop.
Add jitter to latitude/longitude
//
// Make a few assumptions and add noise to latitude/longitude position
// Ex, console.log(jitter(-26.4853429150483, -49.072945734375, 5));
//
var rad_Earth = 6378.16;
var one_degree = (2 * Math.PI * rad_Earth) / 360;
var one_km = 1 / one_degree;
function randomInRange(from, to, fixed) {
fixed = fixed || 10;
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
}
function jitter(lat, lng, kms, fixed) {
return {
lat : randomInRange(
lat - (kms * one_km),
lat + (kms * one_km),
fixed
),
lng : randomInRange(
lng - (kms * one_km),
lng + (kms * one_km),
fixed
)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment