2015-06-14 40 views
8

我使用MKMapView,我送我的PHP程序可見光區(LAT中心,中心經度,緯度跨度,跨度LON)。我需要確定一個座標是否在該區域內使用PHP。我希望有一個標準的公式,但我沒有找到一個。我會繼續努力想出一個公式,但它非常複雜(希望不會像束縛者那樣多,我不相信自己能想出來)。確定是否座標內的區域(的MKMapView,解決PHP)

回答

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

+0

mapView只能從左到右滾動,所以你的中心緯度不能超過89,跨度不能超過2.我基本上只做了兩個區域,如果區域超過了180或-180,那麼它們都會被測試。另外,你的anwswer是有用的,但是這個例子看起來像是在邏輯之外,當它實際在裏面。 ('$ pointLongitude> $ bottomLeftLongitude') –

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

之間我希望它是這麼簡單。我應該澄清一下,'mapView.region'的緯度座標是-90到90,經度-180到180.這是一個棘手的部分。如果點經度爲-179,而區域中心的長度爲179,並且跨度爲20,則上述第三個條件將是錯誤的,但該點仍然可以位於內部。 –

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

我以前工作過我自己的問題的解決方案,但對於十進制值和它的作品。可能是如果你可以將deg轉換爲十進制,它可能會工作。

我已經按照你的問題改名爲變量。

這裏的邏輯。

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