質問

How can I calculate every Lat/Long coordinates between two Lat/Long coordinates in PHP?

Lets say I have coordinates A:

(39.126331, -84.113288) 

and coordinates B:

(39.526331, -84.213288)

How would I calculate every possible coordinates between those two Lat/Long coordinates (in a direct line) up to five decimal places (e.g. 39.12633, -84.11328) and get list of coordinates between the two?

In addition, I have another set of coordinates (Coordinates C) that are slightly off and not on the track of coordinates between A and B.

How could I calculate the distance between coordinates C and the closest coordinates between A and B?

役に立ちましたか?

解決 2

Here is what solved this for me,

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 ) ;
}

他のヒント

You can compute a voronoi diagram from all the lat lon pairs and then look for adjacent cell. Also note that lat lon are angles and not world coordinate or cartesian coordinates. You can download my PHP class voronoi diagram @ phpclasses.org.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top