我应该首先说,在PHP方面我还是很绿,所以请耐心等待我。我试图用php计算两个纬度和经度之间的距离。我在网上发现了一堆脚本,但是当我尝试测试它们时,无论我使用什么纬度和经度,我都会得到相同的距离。我相信这可能是我的意思,所以我只会发布我到目前为止的内容。
$lat1 = "29.140762";
$lon1 = "-91.639243";
$lat2 = "29.136275";
$lon2 = "-91.635524";
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
// Miles
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "m") . " miles<br><br>";
//Kilometers
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "k") . " kilometers<br><br>";
//Nautical miles
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "n") . " nautical miles";
答案 0 :(得分:1)
我猜你已经对输入进行了硬编码,只是更改页面顶部的变量并期待新的结果。那些变量目前没有做任何事情。尝试下面对函数调用所做的更改,现在可以识别变量。
// Miles
echo distance($lat1, $lon1, $lat2, $lon2, "m") . " miles<br><br>";
//Kilometers
echo distance($lat1, $lon1, $lat2, $lon2, "k") . " kilometers<br><br>";
//Nautical miles
echo distance($lat1, $lon1, $lat2, $lon2, "n") . " nautical miles";
答案 1 :(得分:0)
刚试过它并输出
262.67779380543 miles
422.73893139401 kilometers
228.10939614064 nautical miles
Here is another output when using
// Miles
echo distance(35.9697, -96.80322, 29.46786, -98.53506, "m") . " miles<br><br>";
//Kilometers
echo distance(35.9697, -96.80322, 29.46786, -98.53506, "k") . " kilometers<br><br>";
//Nautical miles
echo distance(35.9697, -96.80322, 29.46786, -98.53506, "n") . " nautical miles";
460.32704312505英里
740.82456489105公里
399.7480042498海里