如何仅使用 PHP 检查某个坐标是否落入另一个坐标半径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12439801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:34:46  来源:igfitidea点击:

How to check if a certain coordinates fall to another coordinates radius using PHP only

phpgeolocation

提问by Kenneth Palaganas

I have seen so many functions but it happens to work only for MySQL or Postgresql. I want the equivalent logic for PHP. I'm doing some comparisons, like I have this data that were being produced when created.

我见过很多函数,但它恰好只适用于 MySQL 或 Postgresql。我想要 PHP 的等效逻辑。我正在做一些比较,比如我有这些数据是在创建时生成的。

Lat: 56.130366
Long: -106.34677099999

Later on, I want to check if this coordinates will fall within a radius of another coordinates.

稍后,我想检查这个坐标是否会落在另一个坐标的半径内。

Lat: 57.223366
Long: -106.34675644699
radius: 100000 ( meters )

Thanks in advance!

提前致谢!

回答by Kenneth Palaganas

Thanks for the help. Below is an example function that takes two sets of longitude and latitude co-ordinates and returns the distance between the two.

谢谢您的帮助。下面是一个示例函数,它采用两组经度和纬度坐标并返回两者之间的距离。

function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {  
  $earth_radius = 6371;

  $dLat = deg2rad($latitude2 - $latitude1);  
  $dLon = deg2rad($longitude2 - $longitude1);  

  $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);  
  $c = 2 * asin(sqrt($a));  
  $d = $earth_radius * $c;  

  return $d;  
}

$distance = getDistance(56.130366, -106.34677099999, 57.223366, -106.34675644699);
if ($distance < 100) {
  echo "Within 100 kilometer radius";
} else {
  echo "Outside 100 kilometer radius";
}

回答by Guillaume Poussel

You should use Haversine formulato compute distance between two points. You have a PHP version here.

您应该使用Haversine 公式来计算两点之间的距离。您在此处有一个PHP 版本

Then just check if distance < 100000.

然后检查是否distance < 100000

回答by jeffff

This should help,

这应该有帮助,

$lat_origin = 56.130366;
$long_origin = -106.34677099999;

$lat_dest = 57.223366;
$long_dest = -106.34675644699;

$radius      = 3958;      # Earth's radius (miles, convert to meters)
$deg_per_rad = 57.29578;  # Number of degrees/radian (for conversion)

$distance = ($radius * pi() * sqrt(
            ($lat_origin - $lat_dest)
            * ($lat_origin - $lat_dest)
            + cos($lat_origin / $deg_per_rad)  # Convert these to
            * cos($lat_dest / $deg_per_rad)    # radians for cos()
            * ($long_origin - $long_dest)
            * ($long_origin - $long_dest)
    ) / 180);

回答by Mark Baker

//  Vincenty formula to calculate great circle distance between 2 locations
//      expressed as Lat/Long in KM 

function VincentyDistance($lat1,$lat2,$lon1,$lon2){ 
    $a = 6378137 - 21 * sin(lat); 
    $b = 6356752.3142; 
    $f = 1/298.257223563; 

    $p1_lat = $lat1/57.29577951; 
    $p2_lat = $lat2/57.29577951; 
    $p1_lon = $lon1/57.29577951; 
    $p2_lon = $lon2/57.29577951; 

    $L = $p2_lon - $p1_lon; 

    $U1 = atan((1-$f) * tan($p1_lat)); 
    $U2 = atan((1-$f) * tan($p2_lat)); 

    $sinU1 = sin($U1); 
    $cosU1 = cos($U1); 
    $sinU2 = sin($U2); 
    $cosU2 = cos($U2); 

    $lambda = $L; 
    $lambdaP = 2*PI; 
    $iterLimit = 20; 

    while(abs($lambda-$lambdaP) > 1e-12 && $iterLimit>0) { 
        $sinLambda = sin($lambda); 
        $cosLambda = cos($lambda); 
        $sinSigma = sqrt(($cosU2*$sinLambda) * ($cosU2*$sinLambda) + ($cosU1*$sinU2-$sinU1*$cosU2*$cosLambda) * ($cosU1*$sinU2-$sinU1*$cosU2*$cosLambda)); 

        //if ($sinSigma==0){return 0;}  // co-incident points 
        $cosSigma = $sinU1*$sinU2 + $cosU1*$cosU2*$cosLambda; 
        $sigma = atan2($sinSigma, $cosSigma); 
        $alpha = asin($cosU1 * $cosU2 * $sinLambda / $sinSigma); 
        $cosSqAlpha = cos($alpha) * cos($alpha); 
        $cos2SigmaM = $cosSigma - 2*$sinU1*$sinU2/$cosSqAlpha; 
        $C = $f/16*$cosSqAlpha*(4+$f*(4-3*$cosSqAlpha)); 
        $lambdaP = $lambda; 
        $lambda = $L + (1-$C) * $f * sin($alpha) * ($sigma + $C*$sinSigma*($cos2SigmaM+$C*$cosSigma*(-1+2*$cos2SigmaM*$cos2SigmaM))); 
    } 

    $uSq = $cosSqAlpha*($a*$a-$b*$b)/($b*$b); 
    $A = 1 + $uSq/16384*(4096+$uSq*(-768+$uSq*(320-175*$uSq))); 
    $B = $uSq/1024 * (256+$uSq*(-128+$uSq*(74-47*$uSq))); 

    $deltaSigma = $B*$sinSigma*($cos2SigmaM+$B/4*($cosSigma*(-1+2*$cos2SigmaM*$cos2SigmaM)- $B/6*$cos2SigmaM*(-3+4*$sinSigma*$sinSigma)*(-3+4*$cos2SigmaM*$cos2SigmaM))); 

    $s = $b*$A*($sigma-$deltaSigma); 
    return $s/1000; 
} 


echo VincentyDistance($lat1,$lat2,$lon1,$lon2);