Is it possible to use the jQuery ajax function and the Google directions API to asynchronous calculate the distance in miles between two given addresses? I've found a few examples that show how to do this using PHP, but nothing that doesn't require a page reload
Asked
Active
Viewed 1,062 times
0
-
Why not do it in PHP and use AJAX to get the result from your PHP? – I wrestled a bear once. Oct 14 '14 at 22:16
1 Answers
0
Yes just implement your calculation in php, and call your script asynchronously using ajax:
$.ajax({
url: "calcdist.php?address1="+addr1+"&address2="+addr2,
success: function(result){
alert("Distance is "+result+" miles);
}
});
and in your script (calcdist.php):
<?php
$a1 = $_GET['address1'];
$a2 = $_GET['address2'];
$ll1 = getLatLong($a1);
$ll2 = getLatLong($a2);
$dist = haversine($ll1, $ll2);
echo $dist;
?>
You just need to implement the haversine and getLatLong functions in your php
see Haversine formula with php + http://code.google.com/p/gogeocode/ for help with these