Skip to content

Instantly share code, notes, and snippets.

@jsmarkus
Created August 23, 2012 10:01
Show Gist options
  • Save jsmarkus/3434985 to your computer and use it in GitHub Desktop.
Save jsmarkus/3434985 to your computer and use it in GitHub Desktop.
public function getDistanceTo(Geometry_Point $anotherPoint)
{
//TODO!!!!
if($this->getProjection() !== $anotherPoint->getProjection())
{
throw new Geometry_Exception('Projection mismatch');
}
$lat1 = $this->getLat();
$lon1 = $this->getLng();
$lat2 = $anotherPoint->getLat();
$lon2 = $anotherPoint->getLng();
$R = 6371000; //Radius of the Earth in meters
$dLat = ($lat2-$lat1) * M_PI / 180;
$dLon = ($lon2-$lon1) * M_PI / 180;
$lat1 = $lat1 * M_PI / 180;
$lat2 = $lat2 * M_PI / 180;
$a = sin($dLat / 2) * sin($dLat / 2) +
sin($dLon / 2) * sin($dLon / 2) * cos($lat1) * cos($lat2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $R * $c;
return $d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment