PHPは経緯度に基づいて2点間の距離を計算する2つの方法

10261 ワード

/**
 * @param $lat1
 * @param $lng1
 * @param $lat2
 * @param $lng2
 * @return int
 */
function getDistance($lat1, $lng1, $lat2, $lng2){

    //       

    $radLat1=deg2rad($lat1);//deg2rad()          

    $radLat2=deg2rad($lat2);

    $radLng1=deg2rad($lng1);

    $radLng2=deg2rad($lng2);

    $a=$radLat1-$radLat2;

    $b=$radLng1-$radLng2;

    $s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137;

    return $s;

}

/**
 * @param $lat1
 * @param $lon1
 * @param $lat2
 * @param $lon2
 * @param float $radius      
 * @return float
 */
function distance($lat1, $lon1, $lat2,$lon2,$radius = 6378.137)
{
    $rad = floatval(M_PI / 180.0);

    $lat1 = floatval($lat1) * $rad;
    $lon1 = floatval($lon1) * $rad;
    $lat2 = floatval($lat2) * $rad;
    $lon2 = floatval($lon2) * $rad;

    $theta = $lon2 - $lon1;

    $dist = acos(sin($lat1) * sin($lat2) +
                cos($lat1) * cos($lat2) * cos($theta)
            );

    if ($dist < 0 ) {
        $dist += M_PI;
    }

    return $dist = $dist * $radius;
}
$lat1 = '31.253411';
$lon1 = '121.518998';

$lat2 = '31.277117';
$lon2 = '120.744587';

echo getDistance($lat1, $lon1, $lat2, $lon2);  // 73.734589823361

echo distance($lat1, $lon1, $lat2, $lon2);   // 73.734589823354