php 計算兩點地理座標的距離,php兩點地理座標

來源:互聯網
上載者:User

php 計算兩點地理座標的距離,php兩點地理座標

功能:根據圓周率和地球半徑係數與兩點座標的經緯度,計算兩點之間的球面距離。


擷取兩點座標距離:

<?php/** * 計算兩點地理座標之間的距離 * @param  Decimal $longitude1 起點經度 * @param  Decimal $latitude1  起點緯度 * @param  Decimal $longitude2 終點經度  * @param  Decimal $latitude2  終點緯度 * @param  Int     $unit       單位 1:米 2:公裡 * @param  Int     $decimal    精度 保留小數位元 * @return Decimal */function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){    $EARTH_RADIUS = 6370.996; // 地球半徑係數    $PI = 3.1415926;    $radLat1 = $latitude1 * $PI / 180.0;    $radLat2 = $latitude2 * $PI / 180.0;    $radLng1 = $longitude1 * $PI / 180.0;    $radLng2 = $longitude2 * $PI /180.0;    $a = $radLat1 - $radLat2;    $b = $radLng1 - $radLng2;    $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));    $distance = $distance * $EARTH_RADIUS * 1000;    if($unit==2){        $distance = $distance / 1000;    }    return round($distance, $decimal);}// 起點座標$longitude1 = 113.330405;$latitude1 = 23.147255;// 終點座標$longitude2 = 113.314271;$latitude2 = 23.1323;$distance = getDistance($longitude1, $latitude1, $longitude2, $latitude2, 1);echo $distance.'m'; // 2342.38m$distance = getDistance($longitude1, $latitude1, $longitude2, $latitude2, 2);echo $distance.'km'; // 2.34km?>

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.