Rumah > php教程 > php手册 > teks badan

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

WBOY
Lepaskan: 2016-06-21 08:50:14
asal
1485 orang telah melayarinya
<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>
Salin selepas log masuk
本文链接http://www.cxybl.com/html/wlbc/Php/20130319/37247.html



Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan