Distance between two coordinates in php using haversine

懵懂的女人 提交于 2019-12-11 18:33:25

问题


I've looked around and seen mention of the haversine formula to determine distance between two coordinates (lat1, lng1) and (lat2, lng2).

I've implemented this code:

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

And am trying to determine:

1) what units this is returning? (goal being in feet)

2) is this equation written the right way?

For example what should be the distance between these two points?

(32.8940695525,-96.7926336453) and (33.0642604502, -96.8064332754)?

I'm getting 18968.0903312 from the formula above.

Thanks!


回答1:


1) what units this is returning? (goal being in feet)

Whatever units in which you supply the Earth's radius.

2) is this equation written the right way?

Test it. You can compare your results with an existing Haversine formula implementation, like this one.



来源:https://stackoverflow.com/questions/15140579/distance-between-two-coordinates-in-php-using-haversine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!