Looking to hire Laravel developers? Try LaraJobs

laravel-distance-router maintained by conycom

Description
Calculate road distances using Google Maps or OpenRouteService with automatic caching.
Last update
2026/08/12 23:29 (dev-main)
License
Links
Downloads
0

Comments
comments powered by Disqus

Laravel Distance Router

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.

Run tests

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).