我正在使用MKMapView
,并将可见区域的中心纬度、经度、跨度纬度和跨度经度发送给我的php程序。我需要使用php确定一个坐标是否在该区域内。我希望有一个标准公式可以解决这个问题,但我还没有找到。我会继续尝试想出一个公式,但这似乎很复杂(希望不像haversine那么复杂,我不认为我自己能够解决它)。
我正在使用MKMapView
,并将可见区域的中心纬度、经度、跨度纬度和跨度经度发送给我的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
(
(
($lat - $spanLat) < $centerLat &&
$centerLat < ($lat+ $spanLat)
) &&
(
($long - $spanLong) < $centerLong &&
$centerLong < ($long + $spanLong)
)
)
这个逻辑应该可以工作:
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
$pointLongitude > $bottomLeftLongitude
) - Elliot Alderson