Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i want to calculate distance between 2 selected location from a drop list i have a table id , village_name, lattitude, longitude.

i did follow an example about the calculation but i did not understand very well can anyone help me ??

map.php

<?php 
$village_id ="";
$sql = mysql_query("SELECT lattitude, longitude FROM village WHERE id = '$village_id' ")or die(mysql_error());
$get_row = mysql_fetch_assoc($sql);
$lattitude = $get_row['lattitude'];
$longitude = $get_row['longitude'];

if(isset($_POST['calculate']))
{
    $pt1 = $_POST['pt1'];
    $pt2 = $_POST['pt2'];

}
//function calculate distance
   function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
    {
        $result = "";
        $lattitude = $lat1;
        $lattitude = $lat2;
        $longitude = $lng1;
        $longitude = $lng2;
        var_dump($lattitude);

    $pi80 = M_PI / 180;
    $lat1 *= $pi80;
    $lng1 *= $pi80;
    $lat2 *= $pi80;
    $lng2 *= $pi80;

    $r = 6372.797; // mean radius of Earth in km
    $dlat = $lat2 - $lat1;
    $dlng = $lng2 - $lng1;
    $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $km = $r * $c;

    $result = ($miles ? ($km * 0.621371192) : $km);
    echo $result;
    }

//
?>

<div id="calculate-distance-form">
                <?php require_once('include/select.class.php'); ?>
                   <form action="#" method="post">
                   Location one:
             <select id="location1" name="pt1">
                         <?php echo $opt->Showlocation() ?>
                      </select><br />
                               <br />

              Location two:
             <select id="location2" name="pt2">
                          <option value="0">choose...</option>
                      </select><br />
                               <br />

                      <input type="submit" name="calculate" value="Calculate Distance" />
                </form>
share|improve this question
2  
Possible duplicate of stackoverflow.com/questions/14364908/… –  Ihsan May 4 '13 at 15:38
1  
What is your problem? Dropdown list from database or calculation? –  david strachan May 4 '13 at 17:57
    
@david strachan the calculationnnnnnnn plz help –  user2338686 May 5 '13 at 0:14
    
you can use spherical library and their function computeDistanceBetween developers.google.com/maps/documentation/javascript/… –  cadetill May 6 '13 at 14:33

1 Answer 1

The function uses the Haversine formula to calculate the distances between coordinates.

a = sin²((lat2-lat1)/2) +cos(lat1).cos(lat2).sin²(lon2-lon1/2)

c = 2.atan2(sqrt(a), sqrt(1-a)))

d = R.c

Note that angles need to be in radians to pass to trig functions.

Explanation of function

<?php
function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
    {
        $result = "";
        /* THE 5 LINES BELOW ARE MEANINGLESS
        $lattitude = $lat1;
        $lattitude = $lat2;
        $longitude = $lng1;
        $longitude = $lng2;
        var_dump($lattitude);
        */

    $pi80 = M_PI / 180;// Converts degrees to radians Should be using PHP deg2rad function.



    $lat1 *= $pi80;
    $lng1 *= $pi80;
    $lat2 *= $pi80;
    $lng2 *= $pi80;

    $r = 6372.797; // mean radius of Earth in km
    $dlat = $lat2 - $lat1;//Difference between latitude coordinates
    $dlng = $lng2 - $lng1;//Difference between longitude coordinates
    $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $km = $r * $c;//Distance in km

    $result = ($miles ? ($km * 0.621371192) : $km);//Ternary Operator

    echo $result;
    }
?>  

PHP deg2rad function

Ternary Operator

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
If $miles = true (* 0.621371192) If $miles = false distance is already km*/ 

The function below uses the same formula but is easier to understand

function distanceHaversine($lat1, $lon1, $lat2, $lon2) {
  $delta_lat = $lat2 - $lat1 ;
  $delta_lon = $lon2 - $lon1 ;
  $earth_radius = 3959; // in miles use 6373 for kms
  $alpha = $delta_lat/2;
  $beta  = $delta_lon/2;
  $a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
  $c = asin(min(1, sqrt($a)));
  $distance = 2*$earth_radius * $c;
  $distance = round($distance, 4);

  return $distance;
}

echo $distance = distanceHaversine($lat_1, $lon_1, $lat_2, $lon_2). " Miles";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.