You are here

public function IpGeoLocAPI::addRandomDisplacement in IP Geolocation Views & Maps 8

Return a random point within the circle centered on the supplied location. From: http://stackoverflow.com/a/5838991/5350316 .

Parameters

array $location: Holding 'lat' and 'lon' entries.

float $radius_km: Expressed in meters, should be greater than zero.

File

src/Services/IpGeoLocAPI.php, line 803

Class

IpGeoLocAPI
Class IpGeoAPI to interact with other modules.

Namespace

Drupal\ip_geoloc\Services

Code

public function addRandomDisplacement(array &$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);
}