php - PHP 中 2 点之间的相对位置(纬度/经度)

标签 php distance latitude-longitude direction

我有一个机场数据库,每个点都有纬度和经度。我想运行一个 PHP 脚本来查找给定机场附近的所有机场,以及它们的距离和相对方向。

即机场 KLDJ (40-37-02.810N 074-14-40.539W)

机场附近
KJFK - 约翰·肯尼迪机场 (21.2 海里 NE) (40-38-23.104N 073-46-44.132W)

我使用了来自 http://www.movable-type.co.uk/scripts/latlong.html 的代码寻找距离,并试图用它来寻找方位,这可能是不正确的。

//BEARING RHUMB LINE
$phi = log(tan($lat2/2+pi/4)/tan($lat1/2+pi/4));
$distance['bearing'] = (rad2deg(atan2($theta, $phi)) +180) % 360;

我基本上想通过这个脚本运行所有点并找到我已经知道的距离,然后是方向。 (即 N、S、W、E、NW、SW、NE、SE)

最佳答案

大量借鉴技术here并使用来自 here 的数据, 我把这个例子放在一起

<?php

$chicago = array(
    'lat' => 41.9
  , 'lng' => 87.65
);

$dallas = array(
    'lat' => 32.73
  , 'lng' => 96.97
);

$ftworth = array(
    'lat' => 32.82
  , 'lng' => 97.35
);

$bearing = getBearingBetweenPoints( $dallas, $chicago );

echo "Bearing: $bearing&deg;<br>";
echo "Direction: " . getCompassDirection( $bearing );

function getBearingBetweenPoints( $point1, $point2 )
{
  return getRhumbLineBearing( $point1['lat'], $point2['lng'], $point2['lat'], $point1['lng'] );
}

function getRhumbLineBearing($lat1, $lon1, $lat2, $lon2) {
  //difference in longitudinal coordinates
  $dLon = deg2rad($lon2) - deg2rad($lon1);

  //difference in the phi of latitudinal coordinates
  $dPhi = log(tan(deg2rad($lat2) / 2 + pi() / 4) / tan(deg2rad($lat1) / 2 + pi() / 4));

  //we need to recalculate $dLon if it is greater than pi
  if(abs($dLon) > pi()) {
    if($dLon > 0) {
      $dLon = (2 * pi() - $dLon) * -1;
    }
    else {
      $dLon = 2 * pi() + $dLon;
    }
  }
  //return the angle, normalized
  return (rad2deg(atan2($dLon, $dPhi)) + 360) % 360;
}

function getCompassDirection( $bearing )
{
  static $cardinals = array( 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N' );
  return $cardinals[round( $bearing / 45 )];
}

关于php - PHP 中 2 点之间的相对位置(纬度/经度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2859867/

相关文章:

php - 如果用户已经存在,如何替换一些值?

java - 如何计算当前 View 中是否存在经度/纬度

android - GPS - 在运行和步行时获取距离、时间

php - SQL 选择具有可用日期的属性

php - 使用 PHP/OO 将 ODT 转换为 PDF

php - PHP 默认情况下类的可见性是多少?

android - 如何根据手机加速度计算距离

r - 计算R中点与最近邻居之间的平均距离

javascript - 返回了 Google 距离矩阵 ZERO_RESULTS

mysql - Wordpress 距离搜索 SQL