php的Haversine公式 [英] Haversine formula with php

查看:17
本文介绍了php的Haversine公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use this formula with php. I have a database with some values of latitute and longitude saved.

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

I want to find, with a certain value of latitude and longitude in input, all the distances (in km) from this point with each point in the database. To do this, I used the formula on 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 是我在数据库中的值.

Of course using that in php I replaced radians with deg2rad.The values 37,-122 are my values of input and lat,lng are my values in the database.

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

Below there is my code. The problem is that there is something wrong but I don't understand what. The value of distance is of course wrong.

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

例如值:$lat= 41.9133741000$lng= 12.5203​​944000

For values for example: $lat= 41.9133741000 $lng= 12.5203944000

我有 distance="4826.9341106926" 的输出

I have the output of distance="4826.9341106926"

推荐答案

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

The formula you used, seems to be the arccosine instead of the haversine formula. The haversine formula is indeed more appropriate to calculate the distance on a sphere, because it is less prone to rounding errors.

/**
 * 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.5203​​944000 吗?也许您刚刚使用 $lat=12.5203​​944000 和 $long=0 进行计算,因为您覆盖了 $lat 变量.

P.S. I couldn't find an error in your code, so is it just a typo that you wrote $lat= 41.9133741000 $lat= 12.5203944000 ? Maybe you just calculated with $lat=12.5203944000 and $long=0 because you overwrote your $lat variable.

测试代码并返回正确结果:

Tested the code and it returned a correct result:

$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

这篇关于php的Haversine公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆