为了账号安全,请及时绑定邮箱和手机立即绑定

通过坐标计算 GPS 点之间的距离

通过坐标计算 GPS 点之间的距离

PHP
喵喔喔 2022-01-14 17:09:55
我在通过坐标计算两个 GPS 点之间的距离时遇到了一些麻烦。point ax = 7,2562y = 47,7434599999999point b x = 7,21978y = 47,73836我使用了此处描述的 Haversine 公式。我得到的结果是 4.09 公里。但是,使用这样的工具在地图上定位这些点,我可以测量2.8 公里的距离我尝试的其他几个公式也返回了大约 4 公里的结果。有什么想法我会错过吗?
查看完整描述

1 回答

?
慕仙森

TA贡献1827条经验 获得超8个赞

我认为是因为你以英里为单位使用该功能,在公里中你可以使用类似的东西:


    public static function distance(

        array $from,

        array $to

    ) {

        if (empty($from['lat']) || empty($to['lat'])) {

            return $to['distance'];

        }


        $latitude1  = (float) $from['lat'];

        $latitude2  = (float) $to['lat'];

        $longitude1 = (float) $from['lng'];

        $longitude2 = (float) $to['lng'];


        $theta = $longitude1 - $longitude2;

        $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2)))

            + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)))

        ;

        $distance = acos($distance);

        $distance = rad2deg($distance);

        $distance = $distance * 60 * 1.1515;

        $distance = (is_nan($distance)) ? 0 : $distance * 1.609344;


        return  $distance;

    }


查看完整回答
反对 回复 2022-01-14
  • 1 回答
  • 0 关注
  • 280 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号