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

8

我正在使用MKMapView,并将可见区域的中心纬度、经度、跨度纬度和跨度经度发送给我的php程序。我需要使用php确定一个坐标是否在该区域内。我希望有一个标准公式可以解决这个问题,但我还没有找到。我会继续尝试想出一个公式,但这似乎很复杂(希望不像haversine那么复杂,我不认为我自己能够解决它)。

4个回答

3
让我们试试这个逻辑。
$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

如果你拥有

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

结果
$topRightLongitude = -171;
$bottomLeftLongitude = 169;

所以您的意思是,如果您进行如下测试:

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

1
我的解决方案
$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)
{

}

这似乎适用于 MKMapView,因为区域的纬度始终在-90和90之间。


1
我看了你的代码,感觉有些不对劲。我进行了一个测试,使用中心点(179,89)和跨度(20,20),以及点(-179,-89)。结果显示该点在外面,但实际上该点应该在内部,因为179+10=189,导致-171,而89+10=99,导致-81,所以该点应该在中心点和右上角之间。 - Macerier
地图视图只能左右滚动,因此您永远无法拥有纬度为89且跨度大于2的中心点。我基本上只创建了两个区域并测试了它们,如果该区域横跨180或-180,则会进行测试。另外,您的答案很有用,但是根据该逻辑,示例点看起来应该在外面,而实际上它在里面。($pointLongitude > $bottomLeftLongitude) - Elliot Alderson

0

我之前已经为自己的问题找到了一个解决方案,但是只适用于十进制坐标值。如果您能将度数转换为十进制,它可能会起作用。

我已根据您的问题重命名了变量。

以下是逻辑。

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

0

这个逻辑应该可以工作:

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 ...";
}

我希望它是这么简单。我应该澄清一下,mapView.region的纬度坐标为-90到90,经度为-180到180。这就是棘手的部分。如果点的经度为-179,而区域的中心经度为179,跨度为20,则上述第三个条件将为false,但点仍可能在内部。 - Elliot Alderson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接