计算两个Lat / Long坐标之间的所有坐标到x小数位

时间:2013-08-03 23:03:20

标签: php math gps coordinates geography

如何计算PHP中两个Lat / Long坐标之间的每个Lat / Long坐标?

假设我有坐标A:

(39.126331, -84.113288) 

和坐标B:

(39.526331, -84.213288)

如何计算这两个Lat / Long坐标(直线)中最多五个小数位(例如39.12633,-84.11328)之间的每个可能坐标,并获得两者之间的坐标列表?

另外,我有另一组坐标(坐标C)略微偏离而不在A和B之间的坐标轨道上。

如何计算坐标C与A和B之间最近坐标之间的距离?

2 个答案:

答案 0 :(得分:0)

您可以从所有lat lon对计算voronoi图,然后查找相邻的单元格。还要注意,lat lon是角度而不是世界坐标或笛卡尔坐标。你可以下载我的PHP类voronoi图@ phpclasses.org。

答案 1 :(得分:0)

以下是为我解决的问题,

function point_to_line_segment_distance($startX,$startY, $endX,$endY, $pointX,$pointY)
{

        $r_numerator = ($pointX - $startX) * ($endX - $startX) + ($pointY - $startY) * ($endY - $startY);
        $r_denominator = ($endX - $startX) * ($endX - $startX) + ($endY - $startY) * ($endY - $startY);
        $r = $r_numerator / $r_denominator;

        $px = $startX + $r * ($endX - $startX);
        $py = $startY + $r * ($endY - $startY);

        $closest_point_on_segment_X = $px;
        $closest_point_on_segment_Y = $py;

        $distance = user_bomb_distance_calc($closest_point_on_segment_X, $closest_point_on_segment_Y, $pointX, $pointY);

        return array($distance, $closest_point_on_segment_X, $closest_point_on_segment_Y);
}

function user_bomb_distance_calc($uLat , $uLong , $bLat , $bLong)
{
    $earthRadius = 6371; #km
    $dLat = deg2rad((double)$bLat - (double) $uLat);
    $dlong = deg2rad((double)$bLong - (double) $uLong);

    $a = sin($dLat / 2 ) * sin($dLat / 2 ) + cos(deg2rad((double)$uLat)) * cos(deg2rad((double)$bLat)) * sin($dlong / 2) * sin($dlong / 2);


    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $distance = $earthRadius * $c;

    $meter = 1000; //convert to meter 1KM = 1000M

    return  intval( $distance * $meter ) ;
}