用 PHP 坐标确定地理距离
计算两个地理坐标之间的距离涉及到半正矢或 Vincenty 公式。
半正矢公式
半正矢公式,在以下 PHP 代码中实现:
class CoordDistance { // Latitude and longitude of points A and B public $lat_a, $lon_a, $lat_b, $lon_b; public $earth_radius = 6372.795477598; public $distance; public function DistAB() { $latDelta = $this->lat_b - $this->lat_a; $lonDelta = $this->lon_b - $this->lon_a; $alpha = $latDelta / 2; $beta = $lonDelta / 2; $a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($this->lat_a)) * cos(deg2rad($this->lat_b)) * sin(deg2rad($beta)) * sin(deg2rad($beta)); $c = asin(min(1, sqrt($a))); $distance = 2 * $this->earth_radius * $c; $this->distance = $distance; } }
Vincenty 公式
对于对映点,Vincenty公式更准确:
class CoordDistance { // Latitude and longitude of points A and B public $lat_a, $lon_a, $lat_b, $lon_b; public $earth_radius = 6371000; public $distance; public function DistAB() { // Convert to radians $latFrom = deg2rad($this->lat_a); $lonFrom = deg2rad($this->lon_a); $latTo = deg2rad($this->lat_b); $lonTo = deg2rad($this->lon_b); $lonDelta = $lonTo - $lonFrom; $a = pow(cos($latTo) * sin($lonDelta), 2) + pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta); $angle = atan2(sqrt($a), $b); $distance = $angle * $this->earth_radius; $this->distance = $distance; } }
两个公式都返回距离与所提供的地球半径采用相同的测量单位。
以上是如何使用 PHP 的 Haversine 和 Vincenty 公式计算地理距离?的详细内容。更多信息请关注PHP中文网其他相关文章!