Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate distance between 2 geographical points
#1
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)