How to get directions between two places?

One of the challenges was to get the directions between two places obtained as input from the user.

So we read the Google Directions API provided by the Google Map Web Services. We understood that we needed to get the steps. A step is basically one line of direction, in layman’s terms. So the html_instructions in each step is what we needed.

[php]

$origin=urlencode($_GET[‘origin’]);

$destination=urlencode($_GET[‘destination’]);

//:::::::::::::::: Get the directions from google maps using curl :::::::::::::::::::::

$output="";

$ch= curl_init();

curl_setopt($ch, CURLOPT_URL, "http://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&sensor=false");

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);

$data=curl_exec($ch);

$json_data_var = json_decode($data, true);

//print_r($json_data_var);

[/php]

Lets quickly understand what the above code does? It uses the curl library to send a request to the googleapis in the specified format stores the json response object in the variable named $json_data_var as a array.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s