signed

QiShunwang

“诚信为本、客户至上”

PHP计算2点经纬度之间的距离代码

2021/6/3 16:35:56   来源:
复制代码 代码如下:

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
    $theta = $longitude1 - $longitude2;
    $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
    $miles = acos($miles);
    $miles = rad2deg($miles);
    $miles = $miles * 60 * 1.1515;
    $feet = $miles * 5280;
    $yards = $feet / 3;
    $kilometers = $miles * 1.609344;
    $meters = $kilometers * 1000;
    return compact('miles','feet','yards','kilometers','meters');
}

$point1 = array('lat' => 40.770623, 'long' => -73.964367);
$point2 = array('lat' => 40.758224, 'long' => -73.917404);
$distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
foreach ($distance as $unit => $value) {
    echo $unit.': '.number_format($value,4).'<br />';
}

The example returns the following:

miles: 2.6025
feet: 13,741.4350
yards: 4,580.4783
kilometers: 4.1884
meters: 4,188.3894
您可能感兴趣的文章:
  • php根据地址获取百度地图经纬度的实例方法
  • 定位地理位置PHP判断员工打卡签到经纬度是否在打卡之内
  • golang与php实现计算两个经纬度之间距离的方法
  • PHP计算当前坐标3公里内4个角落的最大最小经纬度实例
  • php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法
  • PHP根据两点间的经纬度计算距离
  • 如何用php根据地址获取经纬度