计算给定2个点,纬度和经度的距离

2021-11-20 00:00:00 latitude-longitude mysql maps

可能的重复:
MySQL 经纬度表设置

我知道这个问题可能已经被问过很多次了,我已经研究了很多,我需要特定事情的帮助.

I know this question has probably been asked many times, I've researched a lot and I need help with specific thing.

假设我有一个表单,用户输入经度和纬度,并且我有一个包含经度和纬度表的数据库,我将如何在该表中搜索半径 15 英里内的一个或多个点?

Lets say I have a form and user enters longitude and latitude, and I have a database which has table containing longitudes and latitudes, how would I search for a point or points in that table that are within 15 miles of radius?

推荐答案

您可以使用计算两点之间距离的公式.例如:

You can use the formula that calculates distances between two points. For example:

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)); 
}

您还可以执行以下操作:

You can also do something like:

$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.";

相关文章