Support Forums

Full Version: Calculate distance between 2 geographical points
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Credits and Source: http://www.roscripts.com/

Name: Calculate distance between 2 geographical points
Description: This simple function will return the distance in Km or Miles between 2 points given their latitude/longitude in degrees.
Snippet:

PHP Code:
<?php
function getDistance($a_lat,$a_lng,$b_lat,$b_lng,$mi=false){
    var 
$radius;
    (
$mi $radius=10253 $radius=6371);
    
$a_lat deg2rad($a_lat);
    
$a_lng deg2rad($a_lng);
    
$b_lat deg2rad($b_lat);
    
$b_lng deg2rad($b_lng);
    if(
$a_lat==$b_lat && $a_lng==$b_lng) return 0;
    if ((
sin($b_lat)*sin($a_lat ) + cos($b_lat)*cos($a_lat )*cos($b_lng-$a_lng))>1)
        return 
$radius acos(1));
     return 
$radius acos(sin($b_lat)*sin($a_lat)+cos($b_lat)*cos($a_lat)*cos($b_lng-$a_lng)));
}
?>



Thankyou for reading. Be happy always Smile