繁体   English   中英

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

[英]Determine if coordinate is inside region (MKMapView, solve in PHP)

我正在使用MKMapView并且将可见区域(中心纬度,中心lon,跨度lat,跨度lon)发送给php程序。 我需要使用php确定坐标是否在该区域内。 我希望某处有一个标准公式,但我还没有找到。 我将继续尝试提出一个公式,但是它出奇的复杂(希望它不像haversine那样多,我不相信自己会想出办法)。

让我们尝试这种逻辑

$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';
}

我的解决方案

$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之间。

此逻辑应该起作用:

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

以前,我曾为自己的问题设计过解决方案,但对于坐标的十进制值却可以解决。 可能是如果您可以将deg转换为十进制,则可能有效。

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

这是逻辑。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM