function ip_geoloc_add_random_displacement in IP Geolocation Views & Maps 8
Same name and namespace in other branches
- 7 ip_geoloc_api.inc \ip_geoloc_add_random_displacement()
Return a random point within the circle centered on the supplied location.
Parameters
array $location, holding 'lat' and 'lon' entries:
float $radius, expressed in meters, should be greater than zero:
Return value
array with randomly displaced 'lat' and 'lon'
From: http://stackoverflow.com/a/5838991/5350316
File
- ./
ip_geoloc_api.inc, line 785 - API functions of IP geolocation module
Code
function ip_geoloc_add_random_displacement(&$location, $radius_km) {
$a = ip_geoloc_random(0, 1);
$b = ip_geoloc_random(0, 1);
if ($b < $a) {
// Swap
$c = $a;
$a = $b;
$b = $c;
}
$angle = 2 * pi() * $a / $b;
// Approximate km to degrees conversion
// See http://stackoverflow.com/questions/1253499/simple-calculations-for-working-with-lat-lon-km-distance
$radius = $radius_km / 111000;
$location['lat'] += $b * $radius * cos($angle);
$location['lon'] += $b * $radius * sin($angle);
}