2012-12-30 4 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로 대체하고 km 인 경우 115.1666667로 대체하십시오. 그것은 단지 용이성을 변환하기위한 것입니다. –

+0

아 물론. 감사. 그것을 시도 할 것이다. – holyredbeard

0

피타고라스를 사용하여 대상과 각 사람 사이의 거리를 차례로 찾고 2km 미만인지 확인합니다. x와 y가 대상의 좌표이고 p, q 중 하나의 좌표가 두 사람 사이의 거리 인 경우 ((xp)^2 + (yq)^2)^(1/2) ^ 상징은 "의 힘에"올렸다는 것을 의미한다.

+1

평면에 점이 있으면 작동하지만, 지리적 좌표 (구의 점)라면 Haversine 함수를 대신 사용해야합니다. –

3

이 함수는 점 1과 점 2의 차이를 계산합니다.이 함수는 마일을 반환하므로 1.609344를 곱하여 킬로미터로 변환합니다.

<?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

관련 문제