PHP uses the longitude and latitude between two points to calculate the straight-line distance between two points

不言
Release: 2023-03-23 10:42:01
Original
3866 people have browsed it

The content of this article introduces how PHP uses the longitude and latitude between two points to calculate the straight-line distance between two points. Now I share it with everyone. Friends in need can refer to it

Recommend a small tool (Coordinate picking, including Baidu Map, Amap, Tencent Map, Google Map), convenient for testing: http://www.gpsspg.com/maps.htm

    /**
     *  计算两组经纬度坐标 之间的距离
     *   params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
     *   return m or km
     */
	 
     function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
       $radLat1 = $lat1 * PI ()/ 180.0;   //PI()圆周率
       $radLat2 = $lat2 * PI() / 180.0;
       $a = $radLat1 - $radLat2;
       $b = ($lng1 * PI() / 180.0) - ($lng2 * PI() / 180.0);
       $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
       $s = $s * 6378.137;
       $s = round($s * 1000);
       if ($len_type --> 1)
       {
           $s /= 1000;
       }
   return round($s, $decimal);
	}
	$Xian = array(
			'lat'=>'34.2620480000',
			'lng'=>'108.9408590000'
			);
	$Chengdu = array(
			'lat'=>'30.6736560000',
			'lng'=>'104.0107450000'
			);
	echo '西安距成都'.GetDistance($Xian['lat'],$Xian['lng'],$Chengdu['lat'],$Chengdu['lng'],2).'km';
Copy after login



The above is the detailed content of PHP uses the longitude and latitude between two points to calculate the straight-line distance between two points. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!