【问题标题】:Calculate distance given 2 points, latitude and longitude [duplicate]计算给定2点,纬度和经度的距离[重复]
【发布时间】:2011-12-22 03:42:20
【问题描述】:

可能重复:
MySQL latitude and Longitude table setup

我知道这个问题可能已经被问过很多次了,我研究了很多,我需要一些具体的帮助。

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

【问题讨论】:

  • 查看右侧“相关”部分的前 10 个链接 ->

标签: mysql maps latitude-longitude


【解决方案1】:

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

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

【讨论】:

  • 在 'where 子句' 中给我一个错误 Unknown column 'distance'
  • 你有什么版本的mysql?尝试将 WHERE 更改为 HAVING。 mysql 5.x 的 where 子句有一些问题。 dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html
  • 为了您的所有信息,MySQL-Query 以英里为单位计算距离。要确定以公里为单位的距离,您必须将其乘以 1.609344(如 PHP 示例中所示)。
  • @claire 这是英里的距离吗?如何将其更改为米?
  • @claire 我会将 *60*1.1515 替换为 *(60*1.1515*1.609344)/1000) 吗?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2016-02-03
    • 2014-12-16
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2012-01-26
    相关资源
    最近更新 更多