Skip to content

Instantly share code, notes, and snippets.

@mitchellhislop
Created December 7, 2010 19:35
Show Gist options
  • Save mitchellhislop/732284 to your computer and use it in GitHub Desktop.
Save mitchellhislop/732284 to your computer and use it in GitHub Desktop.
PHP Google Directions API
<head>
<title>Your Trail</title>
</head>
<body>
<h1>Your Trail</h1>
<?php
echo "<br />";
echo '<h1>Directions:</h1>';
echo '<h2>Start: '.$origin.'</h2>';
foreach ($directions->route as $route)
{
foreach ($route->leg as $leg)
{
foreach ($leg->step as $step)
{
$pos=strpos($step->html_instructions, 'Destination');
if ($pos == false)
{
echo $step->html_instructions. ' for '.$step->distance->text;
echo '<br />';
}
else
{
echo $step->html_instructions;
echo '<br />';
}
}
}
}
echo '<h2>End: '.$dest.'</h2>';
?>
<?php
function directions()
{
//get form data for directions
$origin=$this->input->post('start');
$dest=$this->input->post('end');
$user=$this->session->userdata('trail_id');
$query=$this->db->get_where('trail', array('user_id' => $user));
foreach($query->result() as $result)
{
$query2=$this->db->get_where('locations', array('id' => $result->point));
$url='http://maps.googleapis.com/maps/api/directions/xml?';
$url .= 'origin='.urlencode($origin); //origin from form
$url .= '&destination='.urlencode($dest).'&waypoints='; //destination from form
foreach ($query2->result() as $result)
{
$url.=urlencode($result->address).','.urlencode($result->city).','.urlencode($result->state);
$url.='|';
}
$url=substr($url, 0, -1); //removing the last |
$url .='&sensor=false';
}
//curl the $url
echo $url;
$results=$this->curl->simple_get($url);
$results=new SimpleXMLElement($results);
$data['directions']=$results;
$data['origin']=$origin;
$data['dest']=$dest;
//echo the directions
$this->load->view('directions', $data);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment