Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save camilamoreiradev/3c5aed0a6e49d0f723c09c2fd223fe9d to your computer and use it in GitHub Desktop.
Save camilamoreiradev/3c5aed0a6e49d0f723c09c2fd223fe9d to your computer and use it in GitHub Desktop.
//Função que calcula a distância
function calcDistancia( $lat1 = '', $lon1 = '' , $lat2 = '' , $lon2 = '' , $unidade = '' ) {
if( $lat1 && $lon1 && $lat2 && lon2 ) {
$distancia = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad( $lon1 - $lon2 ));
$distancia = acos($distancia);
$distancia = rad2deg($distancia);
$milhas = $distancia* 60 * 1.1515;
$unidade = strtoupper($unidade);
if ($unidade == "K") {
return ($milhas * 1.609344);
} else if ($unidade == "N") {
return ($milhas * 0.8684);
} else {
return $milhas;
}
} else {
return 'Não foi possível calcular!';
}
}
//Chamada para a função
echo calcDistancia(32.9697, -96.80322, 29.46786, -98.53506, "k")." quilômetros";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment