You are here

function ip_geoloc_add_random_displacement in IP Geolocation Views & Maps 7

Same name and namespace in other branches
  1. 8 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

1 call to ip_geoloc_add_random_displacement()
ip_geoloc_plugin_style_leaflet::render in views/ip_geoloc_plugin_style_leaflet.inc
Transform the View result in a list of marker locations and render on map.

File

./ip_geoloc_api.inc, line 790
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);
}