Código PHP:
<?
function CalculaDistancia($direc1, $direc2)
{
$direccion_google = $direc1;
$resultado = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='.urlencode($direccion_google));
$resultado = json_decode($resultado, TRUE);
$lat = $resultado['results'][0]['geometry']['location']['lat'];
$lng = $resultado['results'][0]['geometry']['location']['lng'];
$direccion_google = $direc2;
$resultado = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='.urlencode($direccion_google));
$resultado = json_decode($resultado, TRUE);
$lat2 = $resultado['results'][0]['geometry']['location']['lat'];
$lng2 = $resultado['results'][0]['geometry']['location']['lng'];
?>
Código Javascript:
Ver original
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry"></script> <script type="text/javascript"> var sevilla = new google.maps.LatLng(<?=$lat;?>, <?=$lng;?>); var buenos_aires = new google.maps.LatLng(<?=$lat2;?>, <?=$lng;?>); var distancia = google.maps.geometry.spherical.computeDistanceBetween(sevilla, buenos_aires); </script>
Código PHP:
<?
return distancia; //se que esto esta mal!!!
}
?>
<?
$p = CalculaDistancia("Avenida del Libertador 420, Merlo, Buenos Aires, Argentina","Avenida del Libertador 520, Merlo, Buenos Aires, Argentina")
?>