【问题标题】:Retrieving items matching a certain GPS coords + distance检索与特定 GPS 坐标 + 距离匹配的项目
【发布时间】:2013-11-11 11:50:40
【问题描述】:

我目前有一个这样的 MySQL 数据库结构(简化):

参赛作品 标题

条目元 纬度 经度

我希望我的用户获取在由坐标 + 距离定义的圆圈内具有 lat/lng 的所有记录(如果这更容易,也可以是正方形)。我只是不知道从哪里开始。有人告诉我有一个数学方程式,但不知道在哪里可以找到。

我正在使用 PHP/MySQL 组合并想查询数据库。该数据库包含大约 100.000 行(到本月底将增加一倍)

【问题讨论】:

标签: php mysql gps latitude-longitude


【解决方案1】:

我目前有适用于这种情况的查询(一个 WordPress 数据库)。祝你好运;)

https://gist.github.com/nielsvr/7412291

【讨论】:

    【解决方案2】:

    以下 SQL 查询使用Spherical Law of Cosines 计算坐标与表中坐标之间的距离。

    d = acos( sin(φ1).sin(φ2) + cos(φ1).cos(φ2).cos(Δλ) ).R

    查询使用SQL Math functions

    "SELECT Id,Lat,Long,(6367 * acos( cos( radians(center_lat) )
      * cos( radians( Lat ) ) * cos( radians( Long ) - radians(center_lng) )
      + sin( radians(center_lat) ) * sin( radians( Lat ) ) ) ) AS distance FROM table 
      HAVING distance < radius ORDER BY distance ASC LIMIT 0 , 20"
    

    尝试使用PDO 代替已弃用的mysql_ 函数

    // Prepare statement
        $stmt = $dbh->prepare("SELECT  name, lat, lng, (6367 * acos( cos( radians(?) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) ) AS distance FROM gbarea HAVING distance < ? ORDER BY distance ASC LIMIT 0 , 20");
        // Assign parameters
        $stmt->bindParam(1,$center_lat);
        $stmt->bindParam(2,$center_lng);
        $stmt->bindParam(3,$center_lat);
        $stmt->bindParam(4,$radius);
        //Execute query
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        while($row = $stmt->fetch()) {
            echo "Name : ".$row['name'].", ";
                echo "Lat : ".$row['lat'].", " ;
                echo "Lng : ".$row['lng'].", ";
                echo "Distance : ".$row['distance'];
            echo "<br>";
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多