数学相关的 PHP 问题 + 纬度

发布于 2024-07-16 07:36:09 字数 358 浏览 6 评论 0原文

我在 PHP 页面中发现一个函数可以计算两点之间的英里数,但它是错误的。 它应该可以与谷歌地图一起使用,但谷歌地图中的距离差异在 1.3 到 1.65 倍之间(更准确)。

这是函数:

$M =  69.09 * rad2deg(acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 - $lon2))));

我发现它有点复杂,而且我对几何了解不多,无法知道这是否正确。

有更多专业知识的人可以看一下这个并找出问题所在吗?

I found a function in a PHP page that calculates the number of miles between 2 points, but it's faulty. It's supposed to work with google maps, but the difference in distances are ranging from 1.3 to 1.65 times further in google maps(which is more accurate).

Here's the function:

$M =  69.09 * rad2deg(acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 - $lon2))));

I find it to be a bit complicated and I don't know that much about geometry to know wheter or not this is correct.

Can someone with a bit more knowhow take a look at this and see what's wrong with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

笔落惊风雨 2024-07-23 07:36:09

也许您正在将“直线距离”(两点之间的直线)与行驶距离进行比较?

另外,请参阅这篇文章以计算 PHP 中两点之间的距离。

Maybe you are comparing the 'distance as the crow flies' (direct line between two points) with driving distance?

Also, see this post for calculating distance between two points in PHP.

千寻… 2024-07-23 07:36:09

您正在寻找半正矢公式来计算您拥有经度和纬度的两点之间的距离为了。

它在 Javascript 中的简单实现可以在这里找到,它应该很容易转换为 PHP。

You're looking for the Haversine formula to calculate distance between two points that you have longitude and latitude for.

A straightforward implementation of it in Javascript can be found here, which should be easy to convert to PHP.

琉璃梦幻 2024-07-23 07:36:09

至少有三种不同的方法来计算地球表面的距离,它们的精度和所需的计算量各不相同。

  1. 余弦球面定律 [不是很
    准确,计算起来非常简单]
  2. Haversine 公式 [除了距离较小之外都很准确,仍然相对简单计算]
  3. Vincenty Formula [高度准确,可以使用几种不同的地球表面的椭球模型,计算起来比较复杂]

你提供的例子似乎是余弦计算定律,而谷歌地图更准确,因为它使用了文森蒂公式。 (我发现 Vincenty 链接比维基百科页面更详细地解释了该公式)

编辑:我看到上面的一条评论,地球表面偏差引入的误差是微不足道的,无法构成您所看到的错误。 恐怕这只有在很远的距离上才是正确的。 在几百公里或更短的距离上,误差绝对是不小的。

There are at least three couple different methods of calculating distance on the surface of the Earth, which vary in accuracy and required computation.

  1. Spherical Law of Cosines [not very
    accurate, very simple to calculate]
  2. Haversine Formula [accurate except at smaller distances, still relatively simple to calculate]
  3. Vincenty Formula [highly accurate and can use several different ellipsoid models of the Earth's surface, more complicated to calculate]

The example you provided appears to be the law of cosines calculation, while Google Maps is more accurate since it uses the Vincenty Formula. (I find that the Vincenty link explains the formula in better detail than it's Wikipedia page)

Edit: I saw a comment above that the error introduced by the deviation in the Earth's surface is trivial and cannot compose the error you are seeing. I'm afraid this is only true over very large distances. At distances of a couple hundred km or less, the errors can be decidedly non-trivial.

傾城如夢未必闌珊 2024-07-23 07:36:09

这是一个更简单的版本,但对于非常远的位置来说并不准确:

    const ONE_DEGREE = 111120;

public function distance( $point ) {
    $coef = cos( $this->getLatitude() / 180 * M_PI );
    $x = $this->getLatitude() - $point->getLatitude();
    $y = ( $this->getLongitude() - $point->getLongitude() ) * $coef;
    $result = sqrt( $x * $x + $y * $y ) * self::ONE_DEGREE;
    return $result;
}

$point 和 $this 是具有 getLatitude() 和 getLongitude() 方法的 Location 类的实例。

Here is a simpler version, but not accurate for very distant locations:

    const ONE_DEGREE = 111120;

public function distance( $point ) {
    $coef = cos( $this->getLatitude() / 180 * M_PI );
    $x = $this->getLatitude() - $point->getLatitude();
    $y = ( $this->getLongitude() - $point->getLongitude() ) * $coef;
    $result = sqrt( $x * $x + $y * $y ) * self::ONE_DEGREE;
    return $result;
}

$point and $this are instances of Location class with getLatitude() and getLongitude() methods.

轻拂→两袖风尘 2024-07-23 07:36:09

我对几何也一无所知,但 Google 建议了此页面。也许你会发现它很有用

I don't know anything about geometry either, but google suggested this page.Maybe you will find it useful

像极了他 2024-07-23 07:36:09

看起来这个公式是准确的 - 例如,参见维基百科上的“大圆距离”。 我认为前面的 69.09 系数是沿着大圆测量的 1 度的英里数(例如赤道 1 度经度的英里),所以你的答案将以英里为单位。

jonstjohn 认为您可能错误地将直线距离与行驶距离进行比较的想法对我来说似乎是最有可能的解释。

编辑:或者如果您正在处理较小的分隔,则可能是维基百科提到的舍入错误。 但我首先会指出直接距离/行驶距离的差异。

It looks like the formula is accurate - see, for example, Wikipedia on "great circle distance". The factor of 69.09 in front is, I believe, the number of miles in one degree measured along a great circle (e.g. miles in 1 degree of longitude at the equator), so your answer will be in miles.

jonstjohn's idea that you might be incorrectly comparing straight-line distance with driving distance seems like the most likely explanation to me.

EDIT: or it could be the rounding error Wikipedia mentions, if you're working with small separations. But I would point my finger at the direct/driving distance difference first.

苏别ゝ 2024-07-23 07:36:09

您引用的计算似乎使用球面坐标系。 公式几乎是正确的。 您使用的半径可能会影响您的计算。 69.09 是球体(在本例中为地球)的半径。 如您所知,地球实际上并不是一个球体,而是一个椭球体。 我建议尝试以下公式:

3963 * acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 - $lon2)));

为了获得更准确的结果,您需要使用 Vincenty 或 Haversine 计算。

编辑:为了澄清,我并不是想暗示您报告的大部分错误是由于使用球坐标计算造成的。 该错误比您看到的要小得多。 我提供的公式调整旨在成为更清晰的公式版本,因为 69.09 是调整为度数系统的地球半径值,这比简单使用弧度更不直观。 此外,值得注意的是,对于计算非常小的距离,只要进行计算的系统具有足够的小数位,使用上面的公式就非常准确(低至约 1m 的距离)。 在现代计算中使用浮点数可以提供这种准确性。

It looks like the calculation you're referencing uses a spherical coordinate system. The formula is almost correct. Part of what could be throwing your calculation off is the radius you're using. The 69.09 is the radius of the sphere (earth in this case). As you may know, the earth isn't really a sphere, more of an ellipsoid. I'd suggest trying the formulation below:

3963 * acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 - $lon2)));

For more accurate results, you'll want to use Vincenty or Haversine calculations.

EDIT: To clarify, I'm not trying to imply that the bulk of the error you're reporting is due to using a spherical coordinate calculation. That error is much smaller than what you're seeing. The formula adjustment I supplied was intended to be a clearer version of the formula, as the 69.09 was a value of the radius of the earth adjusted to a degree system, which is less intuitive than simply using radians. Additionally, it's worth noting that for calculating very small distances, using the formula above is highly accurate (down to about 1m distances) as long as the system doing the calculation is working with enough decimal places. Using a float in modern computing gives you this accuracy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文