0

我正在尝试编写一个 php 函数来计算两点之间的接近度,但我无法让它工作。

我目前正在使用此处找到的功能:http ://www.zipcodeworld.com/samples/distance.php.html

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 == "m") {
    return ($miles * 1.609344)/1000; 
} 
else if ($unit == "N") {
  return ($miles * 0.8684);
} 
else {
    return $miles;
  }
}

我的应用程序需要精确到米,但无论我做什么,我都没有得到正确的结果。

我从这里得到坐标:http: //itouchmap.com/latlong.html

我究竟做错了什么?你能帮我解决这个问题吗?

谢谢

麦克风

4

2 回答 2

3

我刚才写了这个小类,但它应该比你的问题中的方法更好地帮助你计算距离:https ://github.com/treffynnon/Geographic-Calculations-in-PHP

使用上述链接完成您的问题将是:

$Geography = new Geography();
echo $Geography->getDistance($lat1, $lon1, $lat2, $lon2);

将使用Vincenty 的距离计算公式给出以米为单位的距离。

我课堂上使用的 Vincenty 公式是http://www.movable-type.co.uk/scripts/LatLongVincenty.html上提供的 javascript 函数的 PHP 端口

于 2011-02-21T14:30:06.043 回答
0

这是一个页面,详细介绍了 JS 示例可用的算法: http ://www.movable-type.co.uk/scripts/latlong.html

于 2011-02-21T14:34:22.030 回答