确定坐标是否在区域内(MKMapView,用PHP解决)

确定坐标是否在区域内(MKMapView,用PHP解决)

问题描述:

I'm using MKMapView and I send my php program the visible region (center lat, center lon, span lat, span lon). I need to determine if a coordinate is inside that region using php. I'm hoping there's a standard formula somewhere, but I haven't found one. I'll keep trying to come up with a formula, but it's surprisingly complicated (hopefully not as much as the haversine, which I don't believe I could have figured out myself).

我正在使用 MKMapView code>并将我的php程序发送到可见区域(中心lat) ,centre lon,span lat,span lon)。 我需要使用php确定坐标是否在该区域内。 我希望某处有一个标准配方,但我还没找到。 我会继续尝试提出一个公式,但它的复杂程度令人惊讶(希望没有像我所说的那样,我不相信我自己能想出来的。) p> div>

My Solution

$top = $c_lat + ($d_lat / 2.0);
$bottom = $c_lat - ($d_lat / 2.0);
$left = $c_lon - ($d_lon / 2.0);
$right = $c_lon + ($d_lon / 2.0);
if($left < -180)
{
    $second_left = $left + 360.0;
    $second_right = 180.0;
    $left = -180;
}
elseif($right > 180)
{
    $second_right = $right - 360.0;
    $second_left = -180.0;
    $right = 180.0;
}
$inside = false;
if($t_lat > $bottom && $t_lat < $top && $t_lon > $left && $t_lon < $right)
    $inside = true;
else if($second_right && $second_left)
{
    if($t_lat > $bottom && $t_lat < $top && $t_lon > $second_left && $t_lon < $second_right)
        $inside = true;
}

if($inside)
{

}

This seems to work with MKMapView since the region latitudes are always between -90 and 90.

This logic should work:

if  ( ($X > $center_lat - $span_lat/2) &&
      ($X < $center_lat + $span_lat/2) &&
      ($Y > $center_lon - $span_lon/2) &&
      ($Y < $center_lon + $span_lon/2) ) {
    echo "It's inside!";
} else {
    echo "It's outside ...";
}

lets try this logic

$topRightLongitude = $centerLongitude + $spanLongitude/2;
if($topRightLongitude > 180 and ($pointLongitude < 0))
    $topRightLongitude = $topRightLongitude - 360; // (180*2) - positive becomes negative

$bottomLeftLongitude = $centerLongitude - $spanLongitude/2;
if($bottomLeftLongitude< -180 and ($pointLongitude > 0))
    $bottomLeftLongitude= 360 + $bottomLeftLongitude; // now is negative and will become positive

$topRightLatitude = $centerLatitude + $spanLatitude/2;
if($topRightLatitude > 90 and ($pointLatitude < 0))
    $topRightLatitude = $topRightLatitude - 180; // (90*2) - positive becomes negative

$bottomLeftLatitude = $centerLatitude - $spanLatitude/2;
if($bottomLeftLatitude< -90 and ($pointLatitude > 0))
    $bottomLeftLatitude= 180 + $bottomLeftLongitude; // now is negative and will become positive

if you have

$centerLongitude = 179;
$spanLongitude = 20;
$pointLongitude = -179;

results

$topRightLongitude = -171;
$bottomLeftLongitude = 169;

so your point is in if you test like this:

if($pointLongitude < $topRightLongitude &&
    $pointLongitude > $bottomLeftLongitude &&
    $pointLatitude < $topRightLatitude &&
    $pointLatitude > $bottomLeftLatitude){
    echo 'in';
}else{
    echo 'out';
}

I had worked a solution for my own problem before, but for decimal values of coordinates and it works. May be if you can convert deg to decimal it might work.

I have renamed the variable according to your problem.

Here's the logic.

if
(
    (
        ($lat - $spanLat) < $centerLat && 
        $centerLat < ($lat+ $spanLat)
    ) && 
    (
        ($long - $spanLong) < $centerLong && 
        $centerLong < ($long + $spanLong)
    )
)