我见过这么多函数,但它恰好只适用于 MySQL 或 Postgresql。我想要 PHP 的等效逻辑。我正在做一些比较,比如我有这些数据是在创建时生成的。
Lat: 56.130366
Long: -106.34677099999
稍后,我想检查这个坐标是否会落在另一个坐标的半径内。
Lat: 57.223366
Long: -106.34675644699
radius: 100000 ( meters )
提前致谢!
我见过这么多函数,但它恰好只适用于 MySQL 或 Postgresql。我想要 PHP 的等效逻辑。我正在做一些比较,比如我有这些数据是在创建时生成的。
Lat: 56.130366
Long: -106.34677099999
稍后,我想检查这个坐标是否会落在另一个坐标的半径内。
Lat: 57.223366
Long: -106.34675644699
radius: 100000 ( meters )
提前致谢!
谢谢您的帮助。下面是一个示例函数,它采用两组经度和纬度坐标并返回两者之间的距离。
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";
}
您应该使用Haversine 公式来计算两点之间的距离。您在这里有一个PHP 版本。
然后检查是否distance < 100000
。
这应该有帮助,
$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);
// 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);
此函数采用两组纬度、经度,并以指定的单位给出两者之间的距离。
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
if (($lat1 == $lat2) && ($lon1 == $lon2)) {
return 0;
} else {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
}
来源:https ://www.geodatasource.com/developers/php
用法:
distance(32.9697, -96.80322, 29.46786, -98.53506, "M");
最后一个参数是距离单位。可能的单位:
M
为了Miles
K
为了Kilometers
N
为了Nautical Miles