Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created May 9, 2011 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshhartman/962554 to your computer and use it in GitHub Desktop.
Save joshhartman/962554 to your computer and use it in GitHub Desktop.
Calculate the Distance Between Two Geo Coordinates
<?php
// Calculate distance between 2 sets of longitude/latitude coordinates
// Takes two sets of coordinates in decimal longitude and latitude format and returns the distance in miles.
// php, distance, coordinates, longitude, latitude
// Oelwein, IA
$lat1 = 42.6733171;
$lon1 = -91.9135036;
// Independence, IA
$lat2 = 42.4685978;
$lon2 = -91.8893386;
$distance = (3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180);
echo 'The distance from '.$lat1.', '.$lon2.' to '.$lat2.', '.$lon2.' is '.$distance.' mi.';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment