conycom / laravel-distance-router
Calculate road distances using Google Maps or OpenRouteService with automatic caching.
Package info
github.com/conycom/laravel-distance-router
pkg:composer/conycom/laravel-distance-router
Requires
- php: ^8.2
- illuminate/cache: ^10.0|^11.0|^12.0|^13.0
- illuminate/http: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.0|^11.0
This package is auto-updated.
Last update: 2026-08-13 06:12:44 UTC
README
A smart routing and distance calculation package for Laravel. It calculates driving distances (round-trip) seamlessly falling back between OpenRouteService (ORS) and Google Maps, with automatic caching and Spanish Postal Code geocoding.
Features
- Multi-provider support (ORS or Google Maps)
- Automatic coordinate resolution from Spanish Postal Codes (via Nominatim OpenStreetMap)
- Aggressive caching (using Laravel's native Cache) to minimize API costs
- Designed for logistics, ecommerce, and federated sports routing
Installation
composer require conycom/laravel-distance-router
Publish the configuration file:
php artisan vendor:publish --tag="distance-router-config"
Configuration
Set your preferred provider and API keys in your .env file:
DISTANCE_ROUTER_PROVIDER=ors # or 'google' ORS_API_KEY=your-ors-api-key GOOGLE_MAPS_API_KEY=your-google-maps-api-key
Usage
You can pass either coordinate arrays ['lat' => X, 'lon' => Y] or Spanish postal codes (as strings).
The package will automatically geocode the postal codes and cache the result.
use Conycom\DistanceRouter\Facades\DistanceRouter; // Using coordinates $result = DistanceRouter::getRoundTripDistance( ['lat' => 40.4167, 'lon' => -3.7033], // Madrid ['lat' => 41.3851, 'lon' => 2.1734] // Barcelona ); // Using Spanish Postal Codes $result = DistanceRouter::getRoundTripDistance('28001', '08001'); /* $result returns: [ 'success' => true, 'km' => 1240.5, // Total round-trip distance in KM 'duration' => 36000, // Estimated driving duration in seconds 'source' => 'ors' // 'ors', 'google', or 'cache' ] */
License
The MIT License (MIT).