ValueObjects\Geography\Coordinate::distanceFrom PHP Method

distanceFrom() public method

Calculates the distance between two Coordinate objects
public distanceFrom ( Coordinate $coordinate, ValueObjects\Geography\DistanceUnit $unit = null, ValueObjects\Geography\DistanceFormula $formula = null ) : ValueObjects\Number\Real
$coordinate Coordinate
$unit ValueObjects\Geography\DistanceUnit
$formula ValueObjects\Geography\DistanceFormula
return ValueObjects\Number\Real
    public function distanceFrom(Coordinate $coordinate, DistanceUnit $unit = null, DistanceFormula $formula = null)
    {
        if (null === $unit) {
            $unit = DistanceUnit::METER();
        }
        if (null === $formula) {
            $formula = DistanceFormula::FLAT();
        }
        $baseThis = static::getBaseCoordinate($this);
        $baseCoordinate = static::getBaseCoordinate($coordinate);
        $distance = new Distance();
        $distance->setFrom($baseThis)->setTo($baseCoordinate)->in($unit->toNative());
        $value = \call_user_func(array($distance, $formula->toNative()));
        return new Real($value);
    }

Usage Example

Example #1
0
 public function testDistanceFrom()
 {
     $newYork = new Coordinate(new Latitude(41.145556), new Longitude(-73.995));
     $distance = $this->coordinate->distanceFrom($newYork);
     $this->assertSame(7609068.4225575, $distance->toNative());
 }