如何使用緯度和經度來找出半徑內的點?

Susan Sarandon
發布: 2024-10-28 03:21:02
原創
136 人瀏覽過

How to Find Points Within a Radius Using Latitude and Longitude?

確定給定緯度和經度的半徑內的鄰近度

設想一個場景,其中表單允許用戶輸入經度和緯度。同時,資料庫包含一個表格,其中包含地理座標的完整列表。目前的任務涉及識別使用者座標 15 英里半徑內的任何點。

解:

為了實現這一目標,我們利用距離計算的概念使用特定公式在兩點之間進行計算:

function get_distance($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {
    $theta = $longitude1 - $longitude2;
    $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
                (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
                cos(deg2rad($theta)));
    $distance = acos($distance);
    $distance = rad2deg($distance);
    $distance = $distance * 60 * 1.1515;
    switch($unit) {
        case 'Mi':
            break;
        case 'Km' :
            $distance = $distance * 1.609344;
    }
    return (round($distance,2));
}
登入後複製

或者,可以利用資料庫查詢:

$query = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) *
            sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) *
            cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*
            pi()/180))))*180/pi())*60*1.1515
        ) as distance
        FROM `MyTable`
        HAVING distance >= ".$distance.";
登入後複製

以上是如何使用緯度和經度來找出半徑內的點?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!