首页  >  文章  >  数据库  >  如何使用纬度和经度查找半径内的点?

如何使用纬度和经度查找半径内的点?

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