如何通过PHP实现两点地理坐标间的距离计算?
- 内容介绍
- 文章标签
- 相关推荐
本文共计469个文字,预计阅读时间需要2分钟。
原文:本文字例为大家分享了PHP计算两点地理坐标距离的具体代码,供大家参考。具体内容如下:
功能:根据圆周率π和地球半经线系数与两点坐标的经纬度,计算两点之间的球面距离。
获取两点:phpfunction getDistance($latitude1, $longitude1, $latitude2, $longitude2) { $earthRadius=6371; // 地球半径,单位:千米 $theta=$longitude1 - $longitude2; $distance=$earthRadius * sin(deg2rad($latitude1)) * cos(deg2rad($latitude2)) + $earthRadius * cos(deg2rad($latitude1)) * sin(deg2rad($latitude2)) * cos(deg2rad($theta)); return $distance * 1000; // 返回千米为单位的结果}
直接输出结果:php$distance=getDistance(39.9042, 116.4074, 31.2304, 121.4737); // 北京到上海的经纬度echo $distance; // 输出距离,单位:千米
本文实例为大家分享了php计算两点地理坐标距离的具体代码,供大家参考,具体内容如下
功能:根据圆周率和地球半径系数与两点坐标的经纬度,计算两点之间的球面距离。
本文共计469个文字,预计阅读时间需要2分钟。
原文:本文字例为大家分享了PHP计算两点地理坐标距离的具体代码,供大家参考。具体内容如下:
功能:根据圆周率π和地球半经线系数与两点坐标的经纬度,计算两点之间的球面距离。
获取两点:phpfunction getDistance($latitude1, $longitude1, $latitude2, $longitude2) { $earthRadius=6371; // 地球半径,单位:千米 $theta=$longitude1 - $longitude2; $distance=$earthRadius * sin(deg2rad($latitude1)) * cos(deg2rad($latitude2)) + $earthRadius * cos(deg2rad($latitude1)) * sin(deg2rad($latitude2)) * cos(deg2rad($theta)); return $distance * 1000; // 返回千米为单位的结果}
直接输出结果:php$distance=getDistance(39.9042, 116.4074, 31.2304, 121.4737); // 北京到上海的经纬度echo $distance; // 输出距离,单位:千米
本文实例为大家分享了php计算两点地理坐标距离的具体代码,供大家参考,具体内容如下
功能:根据圆周率和地球半径系数与两点坐标的经纬度,计算两点之间的球面距离。

