找出点(lattitude,longtitude)是否在区域内

时间:2015-08-25 15:38:59

标签: geolocation

我有点(lat,long)coordindates,我有区域leftTop(lat,long)rightBottom(lat,long),我需要检查我的点是否在区域内。 仅检查点坐标是否大于leftLat以及更少rightLat是不正确的。 我有角落

 leftLat=81.49021937827182
 leftLng=38.979793936014175

 rightLat=-0.5414380758487521
 rightLng=173.9797962829470

1 个答案:

答案 0 :(得分:0)

function rayCrossesSegment($point, $a, $b)
{
    $px = $point['lng'];
    $py = $point['lat'];
    $ax = $a['lng'];
    $ay = $a['lat'];
    $bx = $b['lng'];
    $by = $b['lat'];
    if ($ay > $by) {
        $ax = $b['lng'];
        $ay = $b['lat'];
        $bx = $a['lng'];
        $by = $a['lat'];
    }
    // alter longitude to cater for 180 degree crossings
    if ($px < 0) {
        $px += 360;
    };
    if ($ax < 0) {
        $ax += 360;
    };
    if ($bx < 0) {
        $bx += 360;
    };

    if ($py == $ay || $py == $by) {
        $py += 0.00000001;
    }
    if (($py > $by || $py < $ay) || ($px > max($ax, $bx))) {
        return false;
    }
    if ($px < min($ax, $bx)) {
        return true;
    }

    $red = ($ax != $bx) ? (($by - $ay) / ($bx - $ax)) : INF;
    $blue = ($ax != $px) ? (($py - $ay) / ($px - $ax)) : INF;

    return ($blue >= $red);

}