PHP code example of alkauni / distance-coordinate-calculator

1. Go to this page and download the library: Download alkauni/distance-coordinate-calculator 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/ */

    

alkauni / distance-coordinate-calculator example snippets


return [
    'default_unit' => 'KM', // Options: 'KM', 'Miles', 'N', 'M'
];

use Alkauni\DistanceCoordinateCalculator\Facades\Distance; // Or DistanceCalculator class

// Calculate distance in Kilometers
$distance = Distance::calculate(-6.200000, 106.816666, -6.917464, 107.619123);
// Output: ~118.44 (KM)

// Specify unit ('Miles', 'KM', 'M', 'N')
$miles = Distance::calculate(40.748817, -73.985428, 34.052235, -118.243683, 'Miles');

$formatted = Distance::calculateFormatted(-6.200000, 106.816666, -6.917464, 107.619123, 'KM', 2);
// Output: "118.44 KM"

$userLat = -6.200000;
$userLon = 106.816666;

$officeLat = -6.205000;
$officeLon = 106.818000;

$isAllowed = Distance::isWithinRadius($userLat, $userLon, $officeLat, $officeLon, 1.0, 'KM');
// Returns: true (user is within 1 KM radius of the office)

$userLat = -6.200000;
$userLon = 106.816666; // Jakarta

$branches = [
    ['name' => 'Surabaya Branch', 'lat' => -7.257472, 'lon' => 112.752088],
    ['name' => 'Bandung Branch', 'lat' => -6.917464, 'lon' => 107.619123],
    ['name' => 'Medan Branch', 'lat' => 3.595196, 'lon' => 98.672223],
];

$nearest = Distance::findNearest($userLat, $userLon, $branches);
// Returns: ['name' => 'Bandung Branch', 'lat' => -6.917464, 'lon' => 107.619123, 'distance' => 118.44]

$sortedBranches = Distance::sortByDistance($userLat, $userLon, $branches, 'lat', 'lon', 'KM', 'asc');
// Returns array ordered from nearest to farthest, each item tagged with calculated 'distance'.

$isValid = Distance::isValidCoordinate(-6.200000, 106.816666); // true
$isInvalid = Distance::isValidCoordinate(95.0, 200.0); // false
bash
php artisan vendor:publish --provider="Alkauni\DistanceCoordinateCalculator\DistanceServiceProvider" --tag="config"