Thursday, September 2, 2010

How to get the distance between to location using the google map?

When we calculate the distance between two lat & long the distance would the air to air so distance is wrong,so you have to send both  lat & long to google to get the driving distance.
Given  below the code for it.
//intiat curl
$ch = curl_init();
//pass the  both start and end lat & long
//check google map doc for option
//mode=driving is for driving distance
//sensor=false for site ,for mobile then it is true
//reponse is json
curl_setopt($ch, CURLOPT_URL,
"http://maps.google.com/maps/api/directions/json?origin=".trim($latitude).",".trim($longitude)."&destination=".$obj->latitude.",".$obj->longitude."&mode=driving&alternatives=false&units=imperial&sensor=false");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $contents = curl_exec ($ch);
$contents=json_decode($contents);
$distance=$contents->routes[0]->legs[0]->distance->text

No comments:

Post a Comment