Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-06-21 08:50:14
Original
1478 Leute haben es durchsucht
<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>
Nach dem Login kopieren
本文链接http://www.cxybl.com/html/wlbc/Php/20130319/37247.html



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage