DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Calculate Distance Between 2 Sets Of Longitude/latitude Coordinates.

05.07.2011
| 4403 views |
  • submit to reddit
        Takes two sets of coordinates in decimal longitude and latitude format and returns the distance in kilometers.

$lat1 = 46.2341938036161; 
$lon1 = -63.124778021257015; 

$lat2 = 46.4141938036161; 
$lon2 = -63.118571124705284; 

$distance = (3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180);

print($distance);