PHP code example of jeroendesloovere / distance

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

    

jeroendesloovere / distance example snippets


use JeroenDesloovere\Distance\Distance;

// first location
$latitude1 = '50.8538510000';
$longitude1 = '3.3550450000';

// second location
$latitude2 = '50.8325600000';
$longitude2 = '3.4787650000';

$distance = Distance::between(
    $latitude1,
    $longitude1,
    $latitude2,
    $longitude2
);

// dump data
echo 'Distance between the two locations = ' . $distance . ' km';

// third location
$latitude3 = '50.8865040000';
$longitude3 = '3.4320850000';

// define multiple items
$items = array(
    array(
        'title' => 'location 2',
        'latitude' => $latitude2,
        'longitude' => $longitude2
    ),
    array(
        'title' => 'location 3',
        'latitude' => $latitude3,
        'longitude' => $longitude3
    )
);

$distance = Distance::getClosest(
    $latitude1,
    $longitude1,
    $items
);

// dump data
echo 'The closest location to location 1 is ' . $distance['title'] . ' and the distance between them is ' . $distance['distance'] . ' km';