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

时间:2011-12-22 03:42:21

标签: mysql maps latitude-longitude

  

可能重复:
  MySQL latitude and Longitude table setup

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

假设我有一个表格,用户输入经度和纬度,我有一个包含经度和纬度的表的数据库,我如何在该表中搜索半径15英里范围内的一个或多个点? / p>

2 个答案:

答案 0 :(得分:42)

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

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

答案 1 :(得分:3)

如果你有两个点的Lat / Lon,你可以得到delta Lat和delta Lon并将其转换为沿纬度的距离和沿经度的距离,并使用毕达哥拉斯定理。

你看过像http://www.movable-type.co.uk/scripts/latlong.html这样的网页了吗?这有几种计算距离的方法。因此,当您浏览点列表时,可以使用公式计算从每个点到感兴趣点的距离,并仅保留满足R <15英里的点。