Wednesday 30 September 2015

get distance between two address through google api in php

<?php
function get_distance($address1,$address2)
{
$address1_encode = urlencode($address1);
$address2_encode = urlencode($address2);
$addresses_string = "?origins=".$address1_encode."&destinations=".$address2_encode."&key=AIzaSyBf6kfop46PNbRUAG-VwHVrGlyvNunpw9o";
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json".$addresses_string;
    $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);
   
  return $response;
    //$status = $response_a->status;
}
if(isset($_POST['get_dis'])){
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$results = get_distance($address1,$address2);
$response_a = json_decode($results);

echo "status :- ";
echo $result_status = $response_a->status;
echo "<br/>";
echo "distance :- ";
echo  $distance = $response_a->rows[0]->elements[0]->distance->value;
echo " M";
}
 ?>
 <form action="" method="post">
 <input type="text" name="address1">&nbsp;&nbsp;&nbsp;
 <input type="text" name="address2">&nbsp;&nbsp;&nbsp;
 <input type="submit" name="get_dis" value="Get Distance">

 </form>

No comments:

Post a Comment