The Directions API uses widop/http-adapter which is a PHP 5.3 library for issuing http requests.
The Google Directions API is a service that calculates directions between locations using an HTTP request. You can search for directions for several modes of transportation, include transit, driving, walking or cycling. Directions may specify origins, destinations and waypoints either as text strings (e.g. "Chicago, IL" or "Darwin, NT, Australia") or as latitude/longitude coordinates. The Directions API can return multi-part directions using a series of waypoints.
use Fungio\GoogleMap\Services\Directions\Directions;
use Widop\HttpAdapter\CurlHttpAdapter;
$directions = new Directions(new CurlHttpAdapter());
If you want to use it with a business account, you can read this documentation.
$response = $directions->route('New York', 'Washington');
The directions service allows you to route a much more advance request. If you want to learn more, you can read this documentation.
When you have requested your direction, the returned object is an Fungio\GoogleMap\Services\Directions\DirectionsResponse
.
It wraps a status & routes.
The available status are defined by the Fungio\GoogleMap\Services\Directions\DirectionsStatus
constants.
// Get the status
$status = $response->getStatus();
A request can return many routes. The directions response wraps an array of
Fungio\GoogleMap\Services\Directions\DirectionsRoute
.
// Get the routes
$routes = $reponse->getRoutes();
A directions route wraps a bound, a copyrights, legs, an overview polyline (encoded), a summary, warnings & waypoint order if you use it in your request.
// Get the routes
$routes = $reponse->getRoutes();
// Iterate each routes
foreach ($routes as $route) {
}
The bound defines the viewport bounding box of this route.
// Get the bound
$bound = $route->getBound();
The copyrights defines the text to be displayed for this route. You must handle and display this information yourself.
// Get the copyrights
$copyrights = $route->getCopyrights();
The directions legs defines an array which contains information about a leg of the route, between two locations within the given route. A separate leg will be present for each waypoint or destination specified. (A route with no waypoints will contain exactly one leg within the legs array.) Each leg consists of a series of steps.
// Get the legs
$legs = $route->getLegs();
The overview polyline is an encoded polyline which represents the route.
// Get the overview polyline
$overviewPolyline = $route->getOverviewPolyline();
The summari is a short textual description for the route, suitable for naming and disambiguating the route from alternatives.
// Get the summary
$summary = $route->getSummary();
It is an array of warnings to be displayed when showing these directions. You must handle and display these warnings yourself.
// Get the warnings
$warnings = $route->getWarnings();
It contains an array indicating the order of any waypoints in the calculated route. This waypoints may be reordered if
the request was passed optimize:true
within its waypoints
parameter.
// Get the waypoint order
$waypointOrder = $route->getWaypointOrder();
A directions legs wraps a distance, a durations, a start location, an end location, a start address, an end address, steps.
// Get the legs
$legs = $route->getLegs();
// Iterate each leg
foreach ($legs as $leg) {
}
It indicates the total distance covered by this leg. It represented by the
Fungio\GoogleMap\Services\Base\Distance
.
// Gets the distance
$duration = $leg->getDistance();
It indicates the total duration of this leg. It is represented by the
Fungio\GoogleMap\Services\Base\Duration
.
// Gets the duration
$duration = $leg->getDuration();
The start location is the coordinate of the start of this leg. It is represented by the
Fungio\GoogleMap\Base\Coordinate
.
// Gets the start location
$startLocation = $leg->getStartLocation();
The end location is the coordinate of the end of this leg. It is represented by the
Fungio\GoogleMap\Base\Coordinate
.
// Gets the end location
$endLocation = $leg->getEndLocation();
It contains the human-readable address (typically a street address) reflecting the start location.
// Gets the start address
$startAddress = $leg->getStartAddress();
It contains the human-readable address (typically a street address) reflecting the end location.
// Gets the end address
$endAddress = $leg->getEndAddress();
A leg contains an array of steps denoting information about each separate step of the leg of the journey.
// Gets the directions steps.
$steps = $leg->getSteps();
A step is the most atomic unit of a direction's route, containing a single step describing a specific, single instruction on the journey. E.g. "Turn left at W. 4th St." The step not only describes the instruction but also contains distance and duration information relating to how this step relates to the following step. For example, a step denoted as "Merge onto I-80 West" may contain a duration of "37 miles" and "40 minutes," indicating that the next step is 37 miles/40 minutes from this step.
A step wraps a distance, a durations, a start location, an end location, instructions, an encoded polyline & a travel mode.
// Gets the directions steps.
$steps = $leg->getSteps();
// Iterate each step
foreach ($steps as $step) {
}
It contains the distance covered by this step until the next step. This field may be undefined if the distance is unknown.
// Gets the distance.
$distance = $step->getDistance();
It contains the typical time required to perform the step, until the next step. This field may be undefined if the duration is unknown.
// Gets the duration.
$duration = $step->getDuration();
It contains the location of the starting point of this step. It is represented by the
Fungio\GoogleMap\Base\Coordinate
.
// Gets the start location.
$startLocation = $step->getStartLocation();
It contains the location of the ending point of this step. It is represented by the
Fungio\GoogleMap\Base\Coordinate
.
// Gets the end location.
$endLocation = $step->getEndLocation();
It contains formatted instructions for this step, presented as an HTML text string.
// Gets the instructions.
$instructions = $step->getInstructions();
It represents the step as an encoded polyline.
// Gets the encoded polyline.
$encodedPolyline = $step->getEncodedPolyline();
It contains the travel mode of this step. it is represented by the Fungio\GoogleMap\Services\Base\TravelMode
constants.
// Gets the travel mode.
$travelMode = $step->getTravelMode();