• Calculate distance between two Locations

    The function below use the latitude and longitude of two locations, and calculate the distance between them in both miles and metric units.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    <!--?php 
    function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
        $theta = $longitude1 - $longitude2;
        $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
        $miles = acos($miles);
        $miles = rad2deg($miles);
        $miles = $miles * 60 * 1.1515;
        $feet = $miles * 5280;
        $yards = $feet / 3;
        $kilometers = $miles * 1.609344;
        $meters = $kilometers * 1000;
        return compact('miles','feet','yards','kilometers','meters'); 
    }
     
    $point1 = array('lat' =--> 18.933333, 'long' =&gt; 74.583333); // Mumbai
    $point2 = array('lat' =&gt; 28.666667, 'long' =&gt; 77.233333); // Delhi
     
    $distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
    foreach ($distance as $unit =&gt; $value) {
        echo $unit.': '.number_format($value,4).'
    ';
    }
     
    ?&gt;
    Tags:

Comments on this post

Leave a Reply

  • Security Code :


    3 + = ten