calculate distance between 2 location selected from a database using php mysql -


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

i did follow example calculation did not understand can me ??

map.php

<?php  $village_id =""; $sql = mysql_query("select lattitude, longitude village 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> 

the function uses haversine formula calculate 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 angles need in radians pass trig functions.

explanation of function

<?php function distance($lat1, $lng1, $lat2, $lng2, $miles = true)     {         $result = "";         /* 5 lines below meaningless         $lattitude = $lat1;         $lattitude = $lat2;         $longitude = $lng1;         $longitude = $lng2;         var_dump($lattitude);         */      $pi80 = m_pi / 180;// converts degrees radians should 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 expr2 if expr1 evaluates true, , expr3 if expr1 evaluates false. if $miles = true (* 0.621371192) if $miles = false distance km*/  

the function below uses same formula easier understand

function distancehaversine($lat1, $lon1, $lat2, $lon2) {   $delta_lat = $lat2 - $lat1 ;   $delta_lon = $lon2 - $lon1 ;   $earth_radius = 3959; // in miles use 6373 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"; 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -