public static function calculateDistance(array $origin, array $destination, $round = 2, $km = false) { if (!isset($origin['latitude']) || !isset($origin['longitude'])) { throw new Exception('The origin coordinates are not set.'); } if (!isset($destination['latitude']) || !isset($destination['longitude'])) { throw new Exception('The destination coordinates are not set.'); } $distance = acos(sin($origin['latitude'] * pi() / 180) * sin($destination['latitude'] * pi() / 180) + cos($origin['latitude'] * pi() / 180) * cos($destination['latitude'] * pi() / 180) * cos(($origin['longitude'] - $destination['longitude']) * pi() / 180)) * 180 / pi() * 60 * 1.1515; $distance = abs(round($distance, $round)); if ($km) { $distance = round($distance * 1.60934, $round); } return $distance; }
public function testCalculateDistance() { $dist = Geo::calculateDistance(array('latitude' => '32.919104', 'longitude' => '-96.77497'), array('latitude' => '30.006003', 'longitude' => '-90.10947'), 2, true); $this->assertEquals(710.11, $dist); }