首頁  >  文章  >  資料庫  >  如何使用緯度和經度來找出半徑內的點?

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

Susan Sarandon
Susan Sarandon原創
2024-10-28 03:21:02137瀏覽

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn