Back to Top

PHP get distance between two places using google maps

Updated 5 years ago

Use curl to use google map api to get the distance.
Here the format of address
$address = $value["street"]." ".$value["region"]." ".$value["country_id"]." ".$value["zip_code"];

Now just paste the code

$url = "http://maps.googleapis.com/maps/api/directions/json?origin=".str_replace(' ', '+', $source_address)."&destination=".str_replace(' ', '+', $destination_address)."&sensor=false";
	            $ch = curl_init();
	            curl_setopt($ch, CURLOPT_URL, $url);
	            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	            curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
	            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	            $response = curl_exec($ch);
	            curl_close($ch);
	            $response_all = json_decode($response);
	            // print_r($response);
	            $distance = $response_all->routes[0]->legs[0]->distance->text;

Get $distance and use
🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Trevin Boucher
    • Trevin Boucher
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home