Home >Backend Development >PHP Tutorial >PHP method to calculate the distance between two coordinates (longitude, latitude), coordinate longitude_PHP tutorial

PHP method to calculate the distance between two coordinates (longitude, latitude), coordinate longitude_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:58979browse

How PHP calculates the distance between two coordinates (longitude, latitude), coordinate longitude

This article describes the example of PHP calculating the distance between two coordinates (longitude, latitude) method. Share it with everyone for your reference. The details are as follows:

Here, PHP is used to calculate the distance between two coordinates (longitude, latitude), and the result is returned in meters or kilometers

function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
{
 $pi80 = M_PI / 180;
 $lat1 *= $pi80;
 $lng1 *= $pi80;
 $lat2 *= $pi80;
 $lng2 *= $pi80;
 $r = 6372.797; // mean radius of Earth in km
 $dlat = $lat2 - $lat1;
 $dlng = $lng2 - $lng1;
 $a = sin($dlat/2)*sin($dlat/2)+cos($lat1)*cos($lat2)*sin($dlng/2)*sin($dlng/2);
 $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
 $km = $r * $c;
 return ($miles ? ($km * 0.621371192) : $km);
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/985268.htmlTechArticlephp method to calculate the distance between two coordinates (longitude, latitude), coordinate longitude This article describes the php calculation in an example Method of distance between two coordinates (longitude, latitude). Share it with everyone...
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