holyredbeard
holyredbeard

Reputation: 21198

Check if coordinates is within a specific distance from other coordinates

Ok, so I have a target on certain coordinates, and some 'persons' on other coordinates, and I want to check if the persons coordinates is within a 2km (2000m) distance from the target coordinates.

The code below is just to illustrate what I want more clear, and the question is of course how could this be done? I would really appreciate a solution to this one, thanks!

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

Upvotes: 3

Views: 3032

Answers (3)

Jimmy Kane
Jimmy Kane

Reputation: 16825

You can do this using Great Circle algorithms. http://en.wikipedia.org/wiki/Great-circle_distance

Here's how to find distance.

EDIT

Here is the full code for you to do that!

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

?>

And of course the results:

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

Upvotes: 4

Get Off My Lawn
Get Off My Lawn

Reputation: 36299

I have this function, it calculates the difference between points 1 and 2. The function returns miles, so I multiplied by 1.609344 to convert it to 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";
    }
}

Upvotes: 4

Mata N
Mata N

Reputation: 26

Use Pythagoras to find the distance between target and each person in turn and check whether it is less than 2 km. If x,y are the coordinates of the target and p,q the coordinates of one of the persons the distance between the two is: ((x-p)^2 + (y-q)^2)^(1/2) The ^ symbol means "raised to the power of".

Upvotes: 0

Related Questions