谁能告诉我使用经度和纬度计算距离的公式是否正确?
就像公式显示的两个城市之间的距离超过 135 公里,这不是真的
代码.php
<?php
$result="";
if(isset($_POST['calculate'], $_POST['pt1'], $_POST['pt2']))
{
$pt1 = $_POST['pt1'];
$pt2 = $_POST['pt2'];
$points = array();
$sql = mysql_query("SELECT longitude, lattitude, village_name FROM village WHERE id = '$pt1' OR id = '$pt2'")or die(mysql_error()); // See how I added the village name column !
$num_row = mysql_num_rows($sql);
if($num_row > 0)
{
while($row = mysql_fetch_array($sql))
{
$points[] = array($row['longitude'], $row['lattitude'], $row['village_name']); // add point to array , [also adding the village_name]
}
}
if(count($points) == 2){ // Check if there are 2 points
$PI = acos(-1);
$lon_s = $points[0][0];
$lon_s = ($lon_s/180)*$PI;
$lat_s = $points[0][1];
$lat_s = ($lat_s/180)*$PI;
$lon_e = $points[1][0];
$lon_e = ($lon_e/180)*$PI;
$lat_e = $points[1][1];
$lat_e = ($lat_s/180)*$PI;
$R = 6371; //km
$A = pow(sin(($lat_e - $lat_s)/2), 2) + cos($lat_s) * cos($lat_e) * pow(sin(($lon_e - $lon_s)/2) , 2);
$C = 2 * atan2(sqrt($A), sqrt(1 - $A));
$D = $R * $C;
$result= 'The distance between <br/>'. $points[0][2] . ' and '. $points[1][2].' <br /> is <br/>'. $D .' '.'km'; // Let's output something fancy
}
}
?>
因为它给出了与现实无关的距离我说的是一个小国