2012-12-30 67 views
1

好的,所以我在某個座標上有一個目標,在另一個座標上有一些「人物」,我想檢查這些人的座標是否在2km(2000m)距目標座標的距離。檢查座標是否在距其他座標的特定距離內

下面的代碼只是爲了說明我想要更清楚些什麼,而問題當然是如何做到的?我真的很感謝這個解決方案,謝謝!

$person0 = Array('56.34342', '49.324523'); 
$person1 = Array('57.49544', '47.421524'); 
$person2 = Array('56.74612', '48.722323'); 

$target = Array('56.35343', '49.342343'); 

for (var $i = 0; $i < 4; i$++) { 
    CheckIfMatch($person + i$); 
} 

function CheckIfMatch($person) { 
    if($person is within 2km from target) { 
     echo 'Match!'; 
    } 
} 
+6

谷歌 「半正矢」 或 「Vincenty」 –

+0

你試過[歐幾里得](http://en.wikipedia.org/wiki/Euclidean_distance)距離? –

回答

2

你可以使用大圓算法來做到這一點。 http://en.wikipedia.org/wiki/Great-circle_distance

以下是如何找到距離。

編輯

這裏是爲你做了完整的代碼!

<?php 
$persons = Array(); 

$persons[] = Array('52.00951','4.36052');//Delft is more than 2 km from den haag 
$persons[] = Array('52.03194','4.31769');//Rijswijk is less than 2 km from den haag 
$persons[] = Array('52.07097','4.29945');//A place near den Haag almost 2 streets from center and my favourite coffee shop 
$persons[] = Array('52.37022','4.89517');//Amsterdamn is about 60 km *DRIVING* from the hagues according to Gmaps 

$target = Array('52.07050', '4.30070');//Den Haag 
$i = 0; 
foreach($persons as $person){ 
    $i++; 
    $distance = calculate_distance($person, $target); 
    if($distance <= 2){ 
     echo "Person $i is within 2km with a distance to taget of $distance</br>"; 
    }else{ 
     echo "Person $i is <b>not</b> within 2km with a distance to taget of $distance</br>"; 
    } 

} 

function calculate_distance($person, $target){ 

    $lat1 = $person[0]; 
    $lng1 = $person[1]; 
    $lat2 = $target[0]; 
    $lng2 = $target[1]; 
    $pi = 3.14159; 
    $rad = doubleval($pi/180.0); 

    $lon1 = doubleval($lng1)*$rad; 
    $lat1 = doubleval($lat1)*$rad; 
    $lon2 = doubleval($lng2)*$rad; 
    $lat2 = doubleval($lat2)*$rad; 
    $theta = $lng2 - $lng1; 

    $dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta)); 

    if ($dist < 0) 
     $dist += $pi; 


    $miles = doubleval($dist * 69.1); 
    $inches = doubleval($miles * 63360); 
    $km = doubleval($dist * 115.1666667); 

    $dist = sprintf("%.2f",$dist); 
    $miles = sprintf("%.2f",$miles); 
    $inches = sprintf("%.2f",$inches); 
    $km = sprintf("%.2f",$km); 
    //Here you can return whatever you please 
    return $km; 
} 

?> 

過程和結果:

Person 1 is not within 2km with a distance to taget of 4.24 
Person 2 is within 2km with a distance to taget of 1.21 
Person 3 is within 2km with a distance to taget of 0.09 
Person 4 is not within 2km with a distance to taget of 41.56 
+0

非常感謝您的回答!我不明白這個因素,你能解釋一下嗎? – holyredbeard

+0

如果您正在使用里程,則將替換系數替換爲69.1,如果是公里,則替換爲115.1666667。這只是爲了轉換易用性 –

+0

啊,當然。謝謝。將嘗試一下。 – holyredbeard

0

使用畢達哥拉斯發現反過來目標和每個人之間的距離,並檢查它是否是不到2公里。如果x,y是目標的座標,並且p,q其中一個人的座標是兩者之間的距離: ((xp)^ 2 +(yq)^ 2)^(1/2) ^符號的意思是「提高到」的力量。

+1

如果它們是飛機中的點,那麼它就可以工作,但如果它們是地理座標(球體上的點),則需要使用Haversine函數。 –

3

我有這個功能,它計算點1和2之間的差異。該函數返回里程,所以我乘以1.609344將其轉換爲km。

<?php 
function lat_long_dist($lat1, $long1, $lat2, $long2){ 
    $pi = pi(); 
    $x = sin($lat1 * $pi/180) * 
      sin($lat2 * $pi/180) + 
      cos($lat1 * $pi/180) * 
      cos($lat2 * $pi/180) * 
      cos(($long2 * $pi/180) - ($long1 * $pi/180)); 
    $x = atan((sqrt(1 - pow($x, 2)))/$x); 
    return (1.852 * 60.0 * (($x/$pi) * 180))/1.609344; 
} 

$people = array(); 
$people[] = array(56.34342, 49.324523); 
$people[] = array(57.49544, 47.421524); 
$people[] = array(56.74612, 48.722323); 

foreach($people as $person){ 
    $lat1 = $person[0]; 
    $lon1 = $person[1]; 
    $distance = lat_long_dist($lat1, $lon1, 56.35343, 49.342343) * 1.609344; 
    if($distance <= 2){ 
     echo "$i is within 2km!\n"; 
    } 
} 
+1

非常感謝您分享您的代碼!將立即嘗試。 – holyredbeard