Home  >  Article  >  Backend Development  >  How to use longitude and latitude in php to calculate the distance between two points (pure code)

How to use longitude and latitude in php to calculate the distance between two points (pure code)

不言
不言Original
2018-08-04 14:17:191942browse

The content of this article is about how PHP uses longitude and latitude to calculate the distance between two points (pure code). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.

function rad($d)
{
       return $d * 3.1415926535898 / 180.0;
}
function GetDistance($lat1, $lng1, $lat2, $lng2)
{
    $EARTH_RADIUS = 6378.137;
    $radLat1 = rad($lat1);
   $radLat2 = rad($lat2);
   $a = $radLat1 - $radLat2;
   $b = rad($lng1) - rad($lng2);
   $s = 2 * asin(sqrt(pow(sin($a/2),2) +
    cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
   $s = $s *$EARTH_RADIUS;
   $s = round($s * 10000) / 10000;
   return $s;
}

Recommended related articles:

How does PHP obtain the code instance of the IP region (pure code)

How does PHP achieve return json data (code)

The above is the detailed content of How to use longitude and latitude in php to calculate the distance between two points (pure code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn