【问题标题】:Haversine formula with php带php的Haversine公式
【发布时间】:2013-01-22 21:40:10
【问题描述】:

我想在 php.ini 中使用这个公式。我有一个保存了一些纬度和经度值的数据库。

我想通过输入的特定纬度和经度值来查找数据库中每个点到该点的所有距离(以公里为单位)。为此,我使用了 googlemaps api 上的公式:

( 6371 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) )

当然在 php 中使用它,我用 deg2rad 替换了弧度。值 37,-122 是我的输入值,而 lat,lng 是我在数据库中的值。

下面是我的代码。问题是出了点问题,但我不明白是什么。距离的值当然是错误的。

//values of latitude and longitute in input (Rome - eur, IT)
$center_lat = "41.8350";
$center_lng =  "12.470";

//connection to database. it works
(..)

//to take each value in the database:
    $query = "SELECT * FROM Dati";
    $result = mysql_query($query);
    while ($row = @mysql_fetch_assoc($result)){
        $lat=$row['Lat']);
        $lng=$row['Lng']);
    $distance =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) * (cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) * (sin(deg2rad($lat))))) );
    }

例如值: $纬度= 41.9133741000 $lng= 12.5203​​944000

我有 distance="4826.9341106926" 的输出

【问题讨论】:

    标签: php google-maps haversine


    【解决方案1】:

    您使用的公式似乎是 arccosine 而不是 haversine 公式。 hasrsine 公式确实更适合计算球体上的距离,因为它不太容易出现舍入误差。

    /**
     * Calculates the great-circle distance between two points, with
     * the Haversine formula.
     * @param float $latitudeFrom Latitude of start point in [deg decimal]
     * @param float $longitudeFrom Longitude of start point in [deg decimal]
     * @param float $latitudeTo Latitude of target point in [deg decimal]
     * @param float $longitudeTo Longitude of target point in [deg decimal]
     * @param float $earthRadius Mean earth radius in [m]
     * @return float Distance between points in [m] (same as earthRadius)
     */
    function haversineGreatCircleDistance(
      $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
    {
      // convert from degrees to radians
      $latFrom = deg2rad($latitudeFrom);
      $lonFrom = deg2rad($longitudeFrom);
      $latTo = deg2rad($latitudeTo);
      $lonTo = deg2rad($longitudeTo);
    
      $latDelta = $latTo - $latFrom;
      $lonDelta = $lonTo - $lonFrom;
    
      $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
        cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
      return $angle * $earthRadius;
    }
    

    附:我在您的代码中找不到错误,所以您写的 $lat= 41.9133741000 $lat= 12.5203944000 只是一个错字吗?也许您刚刚使用 $lat=12.5203​​944000 和 $long=0 进行计算,因为您覆盖了 $lat 变量。

    编辑:

    测试了代码,它返回了正确的结果:

    $center_lat = 41.8350;
    $center_lng = 12.470;
    $lat = 41.9133741000;
    $lng = 12.5203944000;
    
    // test with your arccosine formula
    $distance =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) * (cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) * (sin(deg2rad($lat))))) );
    print($distance); // prints 9.662174538188
    
    // test with my haversine formula
    $distance = haversineGreatCircleDistance($center_lat, $center_lng, $lat, $lng, 6371);
    print($distance); // prints 9.6621745381693
    

    【讨论】:

    • sorry,是的,这只是问题测试中的一个错误,当然在我使用$lat和$lng的代码中
    • 我采用了公式here。使用您的功能,我(使用上面相同的输入)输出“4826934.1106926”。为什么?
    • @user1938352 - 您是否尝试将变量声明为数字而不是字符串?您的示例使用$center_lat = "41.8350";,它应该是$center_lat = 41.8350;,也许它解析了“。”作为千位分隔符,具体取决于您的语言环境设置。
    • 如果我使用 $center_lat = 41.8350;我有相同的输出。我试过了。我很绝望!
    • 我喜欢您将地球半径作为参数的方式 - 非常适合未来。
    【解决方案2】:
    public function getDistanceBetweenTwoPoints($point1 , $point2){
        // array of lat-long i.e  $point1 = [lat,long]
        $earthRadius = 6371;  // earth radius in km
        $point1Lat = $point1[0];
        $point2Lat =$point2[0];
        $deltaLat = deg2rad($point2Lat - $point1Lat);
        $point1Long =$point1[1];
        $point2Long =$point2[1];
        $deltaLong = deg2rad($point2Long - $point1Long);
        $a = sin($deltaLat/2) * sin($deltaLat/2) + cos(deg2rad($point1Lat)) * cos(deg2rad($point2Lat)) * sin($deltaLong/2) * sin($deltaLong/2);
        $c = 2 * atan2(sqrt($a), sqrt(1-$a));
    
        $distance = $earthRadius * $c;
        return $distance;    // in km
    }
    

    【讨论】:

      【解决方案3】:

      来自this link:

      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;
      }
      

      如您所见,这与您的代码有很多不同之处。我不知道您是否对公式有不同的方法,或者转换为 PHP 时的某个步骤出错了,但是上面的公式应该可以工作。

      【讨论】:

      • 这给了我方法的相同输出,所以我认为问题在于输入的值。但错误在哪里?
      【解决方案4】:

      我使用以下存储过程直接在查询中计算距离:

      CREATE FUNCTION GEODIST (lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE)
          RETURNS DOUBLE
          DETERMINISTIC
              BEGIN
                  DECLARE dist DOUBLE;
                  SET dist =  round(acos(cos(radians(lat1))*cos(radians(lon1))*cos(radians(lat2))*cos(radians(lon2)) + cos(radians(lat1))*sin(radians(lon1))*cos(radians(lat2))*sin(radians(lon2)) + sin(radians(lat1))*sin(radians(lat2))) * 6378.8, 1);
                  RETURN dist;
              END|
      

      您只需在 phpMyAdmin 中将上述作为 SQl 语句执行以创建过程。请注意结尾 |,因此在您的 SQL 输入窗口中,选择 |签名为限制器。

      然后在查询中,这样调用它:

      $sql = "
      SELECT `locations`.`name`, GEODIST(`locations`.`lat`, `locations`.`lon`, " . $lat_to_calculate . ", " . $lon_to_calculate . ") AS `distance`
      FROM `locations` ";
      

      我发现这比运行查询后在 PHP 中计算要快得多。

      【讨论】:

      • 顺便说一下,这会以米为单位计算距离,因此您可以将其除以 1000 或以某种方式将其四舍五入以计算 KM。
      【解决方案5】:

      我制作了具有静态功能的haversine类 getDistance 具有四个参数,它返回与机器人位置点的距离

      class HaverSign {
          
           public static 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;
      }
      }
      

      上面的class存放在根目录,根目录包含classes文件夹 在任何 php 页面中使用以下方式调用它

      include "../classes/HaverSign.php";
      $haversign=new HaverSign();
      
      $lat=18.5204;
      $lon=73.8567;
      
      $lat1=18.5404;
      $lon1=73.8167;
      
      $dist = $haversign->getDistance($lat,$lon,$lat1,$lon1);
      echo $dist;
      

      输出如下

      4.7676529976827
      

      【讨论】:

      • 这不是你拼写 Haversine 的方式,仅供参考
      猜你喜欢
      • 2013-06-09
      • 2011-09-12
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      相关资源
      最近更新 更多