【问题标题】:Compare lat/lng against another lat/lng [duplicate]将 lat/lng 与另一个 lat/lng 进行比较 [重复]
【发布时间】:2013-04-13 02:13:21
【问题描述】:

我在数据库中存储了地点及其纬度和经度。

我还有登录用户的经纬度。

我正在开发一种功能,让他们能够在任何给定的测量范围内(比如 20 英里)找到场地。

是否有可能找到距离用户的经纬度 20 英里的最外面的经纬度,然后以某种方式编写查询以在 MySQL 中的距离圈内查找地点?

谢谢。

【问题讨论】:

  • 如果你简单地搜索一下,这个问题就有成千上万个答案:好的关键字是“Haversine”或“Vincenty”
  • 如果您要进行大量此类计算,您可以考虑使用具有空间功能的数据库,如 PostGIS 或 MongoDB,它们将为您完成这些计算。
  • 我看到了@insertusernamehere 链接,但这需要我遍历数据库中的每个值吗?我正在寻找一种方法来计算距离用户位置 20 英里的纬度和经度,然后在数据库中搜索圆圈内的内容!
  • 使用正方形而不是圆形来预选数据。
  • 正方形的角是否会比所需的距离更远?

标签: php mysql


【解决方案1】:

我使用这个(从 javascript 移植到 Objective-C)- 用 javascript http://www.movable-type.co.uk/scripts/latlong.html 表达了很多很棒的位置方程

【讨论】:

    【解决方案2】:

    我找到了一个有效的 PHP 函数:

    function getDistanceBetweenPoints($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));
    
    }  
    

    认为这可能对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 2018-05-20
      • 2011-11-29
      • 1970-01-01
      相关资源
      最近更新 更多