/**
  * Get closest distance from location 1 to one of the two locations (2 and 3)
  */
 public function test()
 {
     // define multiple items
     $items = array(array('title' => 'location 2', 'latitude' => $this->latitude2, 'longitude' => $this->longitude2), array('title' => 'location 3', 'latitude' => $this->latitude3, 'longitude' => $this->longitude3));
     // get closest distance from location 1 to one of the two locations (2 and 3)
     $distance = Distance::getClosest($this->latitude1, $this->longitude1, $items);
     $this->assertEquals('location 3', $distance['title']);
     $this->assertEquals(6.5, $distance['distance']);
 }
Exemple #2
0
 public function printDistances($locations)
 {
     foreach ($locations as $item) {
         $response = GeoLocation::getGeocodeFromGoogle($item);
         $latitude = $response->results[0]->geometry->location->lat;
         $longitude = $response->results[0]->geometry->location->lng;
         $distance = Distance::between($latitude, $longitude, $this->currentLocation->latitude, $this->currentLocation->longitude);
         echo "Distance between {$this->currentLocation->city} and {$item} = " . $distance . "km<br>";
     }
 }
<?php

/**
 * Distance test
 *
 * Get distance between two locations.
 *
 * @author Jeroen Desloovere <*****@*****.**>
 */
require_once __DIR__ . '/../vendor/autoload.php';
use JeroenDesloovere\Distance\Distance;
// first location
$latitude1 = '50.8538510000';
$longitude1 = '3.3550450000';
// second location
$latitude2 = '50.8325600000';
$longitude2 = '3.4787650000';
// 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));
// get distance between two locations
$distance = Distance::between($latitude1, $longitude1, $latitude2, $longitude2);
// dump data
echo 'Distance between the two locations = ' . $distance . ' km';
// get closest distance from location 1 to one of the two locations (2 and 3)
$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';