用PHP的Haversine公式

20 php google-maps haversine

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

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

( 6371 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) )
Run Code Online (Sandbox Code Playgroud)

当然在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))))) );
    }
Run Code Online (Sandbox Code Playgroud)

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

我的输出距离="4826.9341106926"

mar*_*kli 50

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

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

PS我在你的代码中找不到错误,所以它只是你写的错字$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
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢你如何将地球半径作为参数 - 非常适合未来. (4认同)

Jak*_*ner 5

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
}
Run Code Online (Sandbox Code Playgroud)