1. Go to this page and download the library: Download abdullmng/laravel-distance 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/ */
abdullmng / laravel-distance example snippets
use Abdullmng\Distance\Facades\Distance;
// Calculate straight-line distance between two addresses
$result = Distance::between(
'New York, NY',
'Los Angeles, CA'
);
echo $result->distance; // Distance in default unit (km)
echo $result->inMiles(); // Convert to miles
echo $result->inMeters(); // Convert to meters
// Calculate actual driving distance using road networks
$route = Distance::route(
'Lagos, Nigeria',
'Abuja, Nigeria'
);
echo $route->distance; // Distance in default unit (km)
echo $route->duration; // Duration in seconds
echo $route->formattedDuration(); // e.g., "6h 30m"
echo $route->inMiles(); // Convert to miles
use Abdullmng\Distance\DTOs\Coordinate;
// Using Coordinate objects
$from = new Coordinate(40.7128, -74.0060); // New York
$to = new Coordinate(34.0522, -118.2437); // Los Angeles
// Straight-line distance
$result = Distance::between($from, $to);
// Route distance
$route = Distance::route($from, $to);
// Using coordinate strings
$result = Distance::between(
'40.7128,-74.0060',
'34.0522,-118.2437'
);
// Calculate in miles
$result = Distance::between('New York, NY', 'Los Angeles, CA', 'miles');
// Or use the unit() method
$result = Distance::unit('miles')->between('New York, NY', 'Los Angeles, CA');