Home > php教程 > php手册 > body text

坐标距离计算:php 计算 两个坐标之间的距离

WBOY
Release: 2016-06-21 08:50:14
Original
1478 people have browsed it
<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li><span>define(<span class="string">'EARTH_RADIUS'</span><span>, 6378.137);</span><span class="comment">//地球半径</span><span> </span></span></li>
<li class="alt"><span>define(<span class="string">'PI'</span><span>, 3.1415926); </span></span></li>
<li><span><span class="comment">/**</span> </span></li>
<li class="alt"><span><span class="comment">* 计算两组经纬度坐标 之间的距离</span> </span></li>
<li><span><span class="comment">* params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);</span> </span></li>
<li class="alt"><span><span class="comment">* return m or km</span> </span></li>
<li><span><span class="comment">*/</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> GetDistance(</span><span class="vars">$lat1</span><span>, </span><span class="vars">$lng1</span><span>, </span><span class="vars">$lat2</span><span>, </span><span class="vars">$lng2</span><span>, </span><span class="vars">$len_type</span><span> = 1, </span><span class="vars">$decimal</span><span> = 2) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span><span class="vars">$radLat1</span><span> = </span><span class="vars">$lat1</span><span> * PI / 180.0; </span></span></li>
<li><span><span class="vars">$radLat2</span><span> = </span><span class="vars">$lat2</span><span> * PI / 180.0; </span></span></li>
<li class="alt"><span><span class="vars">$a</span><span> = </span><span class="vars">$radLat1</span><span> - </span><span class="vars">$radLat2</span><span>; </span></span></li>
<li><span><span class="vars">$b</span><span> = (</span><span class="vars">$lng1</span><span> * PI / 180.0) - (</span><span class="vars">$lng2</span><span> * PI / 180.0); </span></span></li>
<li class="alt"><span><span class="vars">$s</span><span> = 2 * asin(sqrt(pow(sin(</span><span class="vars">$a</span><span>/2),2) + </span><span class="func">cos</span><span>(</span><span class="vars">$radLat1</span><span>) * </span><span class="func">cos</span><span>(</span><span class="vars">$radLat2</span><span>) * pow(sin(</span><span class="vars">$b</span><span>/2),2))); </span></span></li>
<li><span><span class="vars">$s</span><span> = </span><span class="vars">$s</span><span> * EARTH_RADIUS; </span></span></li>
<li class="alt"><span><span class="vars">$s</span><span> = </span><span class="func">round</span><span>(</span><span class="vars">$s</span><span> * 1000); </span></span></li>
<li><span><span class="keyword">if</span><span> (</span><span class="vars">$len_type</span><span> > 1) </span></span></li>
<li class="alt"><span>{ </span></li>
<li><span><span class="vars">$s</span><span> /= 1000; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="keyword">return</span><span> </span><span class="func">round</span><span>(</span><span class="vars">$s</span><span>, </span><span class="vars">$decimal</span><span>); </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="func">echo</span><span> GetDistance(39.908156,116.4767, 39.908452,116.450479, 1);</span><span class="comment">//输出距离/米 </span><span> </span></span></li>
</ol>
Copy after login
本文链接http://www.cxybl.com/html/wlbc/Php/20130319/37247.html



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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template