【问题标题】:haversine distance - MySQL and PHP return different results for same formulahasrsine distance - MySQL 和 PHP 为相同的公式返回不同的结果
【发布时间】:2012-09-23 09:42:02
【问题描述】:

距离(直线)

来自:(纬度)48.73233(经度)2.36618

到:lat() 48.84647(长)2.41026

等于:13096.16 米

如果我使用 PHP 公式,我会得到正确的结果。

但是当我将相同的 PHP 公式直接转换为 MySQL 查询时 - 我得到 5904.2757 等。

代码如下:

php:

$distance = atan2(sqrt(pow(sin((($to_lat - $from_lat) * M_PI / 180) / 2), 2) + cos(($from_lat * M_PI / 180)) * cos(($to_lat * M_PI / 180)) * pow(sin((($to_long - $from_long) * M_PI / 180) / 2), 2)), sqrt(1 - (pow(sin((($to_lat - $from_lat) * M_PI / 180) / 2) , 2) + cos(($from_lat * M_PI / 180)) * cos(($to_lat * M_PI / 180)) * pow(sin((($to_long - $from_long) * M_PI / 180) / 2), 2)))) * 2 * $radiusOfEarth;

mysql:

atan2(sqrt(pow(sin(((ap.Latitude - $from_lat) * pi() / 180) / 2), 2) + cos(($from_lat * pi() / 180)) * cos((ap.Latitude * pi() / 180)) * pow(sin(((ap.Longitude - $from_long) * pi() / 180) / 2), 2)), sqrt(1 - (pow(sin(((ap.Latitude - $from_lat) * pi()) / 180) / 2), 2) + cos(($from_lat * pi() / 180)) * cos((ap.Latitude * pi() / 180)) * pow(sin(((ap.Longitude - $from_long) * pi() / 180) / 2), 2)))) * 2 * 6371000 作为距离

【问题讨论】:

    标签: mysql distance haversine


    【解决方案1】:

    你想要的东西。

    SELECT ((ACOS(SIN(48.73233 * PI() / 180) * SIN(48.84647 * PI() / 180) + COS(48.73233 * PI() / 180) *
    COS(48.84647 * PI() / 180) * COS((2.36618 - 2.41026) * PI() / 180)) * 180 / PI()) * 60 *1.1515 * 1.609344 *1000)
    AS distance FROM dual;
    

    【讨论】:

    • 仍然给我相同的米结果:5904.27
    • 我的错,我忘记了公式中的公里数。将最终结果乘以 * 1.609344 并得到 KM,然后将其乘以 1000,您将得到所需的精确米。
    • 转换没有 qork.. 还是有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多