Created
January 23, 2020 17:09
-
-
Save camilamoreiradev/3c5aed0a6e49d0f723c09c2fd223fe9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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