首页 > php教程 > php手册 > 正文

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

WBOY
发布: 2016-06-21 08:50:14
原创
1477 人浏览过
<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>
登录后复制
本文链接http://www.cxybl.com/html/wlbc/Php/20130319/37247.html



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板