0

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

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116

1 Answers1

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

Community
  • 1
  • 1
markt
  • 903
  • 7
  • 21