PHP code example of gowelle / laravel-route-matrix
1. Go to this page and download the library: Download gowelle/laravel-route-matrix library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$response = GoogleRoutes::from('Julius Nyerere International Airport, Dar es Salaam')
->to('Mlimani City Mall, Dar es Salaam')
->get();
use Gowelle\LaravelRouteMatrix\ValueObjects\Waypoint;
// Example with Place IDs
$response = GoogleRoutes::from(Waypoint::fromPlaceId('ChIJ...' /* Posta */))
->to(Waypoint::fromAddress('Mlimani City Mall, Dar es Salaam'))
->get();
use Gowelle\LaravelRouteMatrix\Enums\TravelMode;
// Using enum
$response = GoogleRoutes::from($origin)
->to($destination)
->travelMode(TravelMode::DRIVE)
->get();
// Using shortcuts
$response = GoogleRoutes::from($origin)
->to($destination)
->driving() // or walking(), bicycling(), transit()
->get();
use Gowelle\LaravelRouteMatrix\Enums\RoutingPreference;
$response = GoogleRoutes::from($origin)
->to($destination)
->routingPreference(RoutingPreference::TRAFFIC_AWARE_OPTIMAL)
->get();
// Or use the shortcut
$response = GoogleRoutes::from($origin)
->to($destination)
->withOptimalTraffic()
->get();
$response = GoogleRoutes::from($origin)
->to($destination)
->withAlternatives()
->get();
// Get the main route
$mainRoute = $response->first();
// Get alternative routes
$alternatives = $response->getAlternatives();
foreach ($alternatives as $route) {
echo "Alternative: {$route->getFormattedDuration()}\n";
}
use Gowelle\LaravelRouteMatrix\Enums\PolylineEncoding;
$response = GoogleRoutes::from($origin)
->to($destination)
->highQualityPolyline()
->get();
// Or use GeoJSON format
$response = GoogleRoutes::from($origin)
->to($destination)
->geoJsonPolyline()
->get();
use Gowelle\LaravelRouteMatrix\Enums\Units;
$response = GoogleRoutes::from($origin)
->to($destination)
->language('es-ES')
->region('ES')
->units(Units::METRIC) // or imperial()
->get();
use Gowelle\LaravelRouteMatrix\Facades\GoogleRoutes;
use Gowelle\LaravelRouteMatrix\Enums\TravelMode;
use Gowelle\LaravelRouteMatrix\Enums\RoutingPreference;
use Carbon\Carbon;
// Posta to Mlimani City with waypoints and options
$response = GoogleRoutes::from(['lat' => -6.8163, 'lng' => 39.2807])
->to(['lat' => -6.7724, 'lng' => 39.2083])
->via(['lat' => -6.8235, 'lng' => 39.2695]) // Kariakoo
->travelMode(TravelMode::DRIVE)
->routingPreference(RoutingPreference::TRAFFIC_AWARE_OPTIMAL)
->avoidTolls()
->departureTime(Carbon::now()->addHour())
->withAlternatives()
->withFuelEfficientRoute()
->language('en-US')
->metric()
->fields([
'routes.duration',
'routes.distanceMeters',
'routes.polyline.encodedPolyline',
'routes.legs',
'routes.routeLabels',
])
->get();
// Process the response
$route = $response->first();
echo "Distance: " . $route->getDistanceInKilometers() . " km\n";
echo "Duration: " . $route->getFormattedDuration() . "\n";
echo "Polyline: " . $route->polyline?->encodedPolyline . "\n";
// Check for warnings
if (!empty($route->warnings)) {
foreach ($route->warnings as $warning) {
echo "Warning: {$warning}\n";
}
}
use Illuminate\Database\Eloquent\Model;
use Gowelle\LaravelRouteMatrix\Contracts\Routable;
use Gowelle\LaravelRouteMatrix\Traits\HasRoute;
class Store extends Model implements Routable
{
use HasRoute;
// Optional: Customize how the waypoint is resolved
// (Defaults to looking for lat/lng, latitude/longitude, or address attributes)
}
$store = Store::find(1);
$customer = User::find(5); // Assuming User also implements Routable
$response = GoogleRoutes::from($store)
->to($customer)
->get();
// Find which store/warehouse is closest to a customer
$response = GoogleRoutes::matrix()
->addOrigin(['lat' => -6.8163, 'lng' => 39.2807]) // Store A (Posta)
->addOrigin(['lat' => -6.8235, 'lng' => 39.2695]) // Store B (Kariakoo)
->addOrigin(['lat' => -6.7724, 'lng' => 39.2083]) // Store C (Mlimani City)
->addDestination(['lat' => -6.7567, 'lng' => 39.2772]) // Customer (Masaki)
->driving()
->get();
$closestStore = $response->getClosestOrigin(0);
echo "Ship from store at origin index: {$closestStore->originIndex}";
// Get all destinations sorted by distance (closest first)
$sortedByDistance = $response->sortedByDistance();
// Get all destinations sorted by duration (fastest first)
$sortedByDuration = $response->sortedByDuration();
// Get only elements where a route was found
$validRoutes = $response->withRoutes();
use Gowelle\LaravelRouteMatrix\Exceptions\GoogleRoutesException;
use Gowelle\LaravelRouteMatrix\Exceptions\InvalidApiKeyException;
use Gowelle\LaravelRouteMatrix\Exceptions\InvalidRequestException;
use Gowelle\LaravelRouteMatrix\Exceptions\NoRouteFoundException;
try {
$response = GoogleRoutes::from($origin)
->to($destination)
->get();
} catch (InvalidApiKeyException $e) {
// API key is missing or invalid
} catch (InvalidRequestException $e) {
// Request parameters are invalid
} catch (NoRouteFoundException $e) {
// No route could be found
} catch (GoogleRoutesException $e) {
// Other API errors
}