douzinei6926 2012-03-08 23:14
浏览 43
已采纳

伴随球面定律的不准确...特别是沿纬度?

I have a strange problem using the Cosine formula in my PHP application..

function CalculateDistanceCosine($decA, $decB)
{
    $lon1 = $decA[0]; //This would be equal to point A's longitude, and so on..
    $lat1 = $decA[1]; 
    $lon2 = $decB[0];   
    $lat2 = $decB[1];

    //echo $lon1." ".$lat1."<br/>";
    //echo $lon2." ".$lat2."<br/>";

    $distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2-$lon1));
    $distance  = acos($distance);
    $distance  = rad2deg($distance);
    $distance  = $distance * 60 * 1.1515;
    $distance  = round($distance, 4);

    return $distance;   
}

My input into this would be something like this:

45.468055555556 -73.741388888889 //- The coordinates for Montreal International Airport
28.428888888889 -81.315833333333 //- Orlando International Airport

However, upon using it, I get wild mistakes.. i.e, "The distance from Montreal to Orlando is 576 KM -- very wrong."

What's interesting is that it is very accurate along the longitude axis. For example, if I gave an input of:

50 -73.741388888889 
50 -81.315833333333 

The error is now only about 50KM, very acceptable.

In other words, why is it neglecting latitudinal differences?

I've tried the Harvesine formula with similar results unfortunately.

  • 写回答

2条回答 默认 最新

  • drduh44480 2012-03-08 23:58
    关注

    I believe your main problem is that you're getting latitude confused with longitude. You've not shown us the code that's wrong, but bear in mind that in your arrays, you would need the longitude first, followed by the latitude, for your function to work. In the example you've given, you've listed the latitude first, followed by the longitude.

    Your code would certainly work out the distance between those two points as written as 576 (in units I'll mention in a minute), but those points aren't actually where you think they are :) Try changing your function as follows:

    $lat1 = $decA[0]; //This would be equal to point A's *latitude*, and so on..
    $lon1 = $decA[1]; 
    $lat2 = $decB[0];   
    $lon2 = $decB[1];
    

    ...or just pass in the values in the expected order.

    Also, I don't recognise your distance multiplier, but it looks like you might be calculating your result in miles, not kilometres. For kilometres, try:

    $distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2-$lon1));
    $distance  = acos($distance);
    $distance  = $distance * 6372.8;
    $distance  = round($distance, 4);
    

    (It's only skipping the conversion back to degrees and the multiplier that's changed, the basic formula remains the same.)

    Given the above changes, your distance works out at around 2,008 kilometres. Is that about right? I'm afraid I've never visited Montreal, Orlando, or anywhere in between...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加