Skip to content

Instantly share code, notes, and snippets.

@fabianofa
Created August 25, 2016 18:33
Show Gist options
  • Save fabianofa/ad6c107856d4ee6eabff752aa973bdc0 to your computer and use it in GitHub Desktop.
Save fabianofa/ad6c107856d4ee6eabff752aa973bdc0 to your computer and use it in GitHub Desktop.
Gera valor de latitude e longitude até uma determinada distância de um ponto inicial.
<?php
$randomCoordinated = function($x0, $y0, $radius){
$randomFloat = function($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
};
// Convert radius from meters to degrees
$radiusInDegrees = $radius / 111000;
$u = $randomFloat();
$v = $randomFloat();
$w = $radiusInDegrees * sqrt($u);
$t = 2 * pi() * $v;
$x = $w * cos($t);
$y = $w * sin($t);
// Adjust the x-coordinate for the shrinking of the east-west distances
$new_x = $x / cos($y0);
$foundLongitude = $new_x + $x0;
$foundLatitude = $y + $y0;
$object = new StdClass();
$object->lat = $foundLongitude;
$object->lng = $foundLatitude;
return $object;
};
// Usage:
// Ex: Algum ponto em Florianópolis.
$randomLatitude = $randomCoordinated(-27.59465, -48.53699, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment