1

我想从下拉列表中计算 2 个选定位置之间的距离,我有一个表 ID、村庄名称、纬度、经度。

我确实遵循了一个关于计算的例子,但我不太明白,谁能帮助我??

地图.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>
4

1 回答 1

1

该函数使用 Haversine 公式计算坐标之间的距离。

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

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

d = R.c

请注意,角度需要以弧度为单位才能传递给三角函数。

功能说明

<?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 函数

三元运算符

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*/ 

下面的函数使用相同的公式,但更容易理解

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";
于 2013-05-05T12:08:09.853 回答