如何在php中找到两组坐标之间的距离

时间:2017-05-09 11:21:59

标签: php

假设我有两组坐标,我从地图源到android应用程序得到这些坐标并将其发送到服务器,现在服务器端将计算它们之间的距离并打印。

2 个答案:

答案 0 :(得分:0)

试试这个:

function calculateDistance($lat1, $lon1, $lat2, $lon2, $unit) {

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}

你可以这样称呼:

calculateDistance($latitude1, $longitude1, $latitude2, $longitude2, "K");

Reference

答案 1 :(得分:0)

试试这个

<?php

$points = array
(
    "point1" => "0,55" ,
    "point2" => "0,52" 
);

function get_distance($inputs)
{
    $count = 1;
    foreach($inputs as $x => $y)
    {
        echo $x." = (".$y.")<br>";
        $p[$count] = explode(",",$y);
        $count++;
    }

    $x1 = $p[1][0];
    $y1 = $p[1][1];

    $x2 = $p[2][0];
    $y2 = $p[2][1];

    $distance = sqrt(pow(($x2 - $x1) , 2) + pow(($y2 - $y1) , 2));
    echo $distance;
}

get_distance($points);

?>