您可以使用 Great Circle 算法来做到这一点。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