ホームページ >php教程 >php手册 >座標距離の計算: PHP は 2 つの座標間の距離を計算します。

座標距離の計算: PHP は 2 つの座標間の距離を計算します。

WBOY
WBOYオリジナル
2016-06-21 08:50:141516ブラウズ
<ol class="dp-c">
<li class="alt"><span><span><?php </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



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。