Home > Backend Development > PHP Tutorial > 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)

不言
Release: 2023-04-03 15:50:02
Original
2125 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template