计算给定2点,纬度和经度的距离[重复]

时间:2022-12-31 16:02:33

Possible Duplicate:
MySQL latitude and Longitude table setup

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

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

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

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?

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

2 个解决方案

#1


42  

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

#2


3  

If you have the Lat/Lon of two points, you can get delta Lat and delta Lon and convert this to a distance along latitude and a distance along longitude, and use Pythagorean theorem.

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

Have you looked at pages like http://www.movable-type.co.uk/scripts/latlong.html? This has several ways of computing distance. So as you go through your list of points, you use the formula to calculate the distance from each point to the point of interest and keep only those that satisfy R<15 miles.

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

#1


42  

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

#2


3  

If you have the Lat/Lon of two points, you can get delta Lat and delta Lon and convert this to a distance along latitude and a distance along longitude, and use Pythagorean theorem.

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

Have you looked at pages like http://www.movable-type.co.uk/scripts/latlong.html? This has several ways of computing distance. So as you go through your list of points, you use the formula to calculate the distance from each point to the point of interest and keep only those that satisfy R<15 miles.

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