【问题标题】:how to define a radial search area around a lat/lng position?如何在 lat/lng 位置周围定义径向搜索区域?
【发布时间】:2012-01-16 11:37:20
【问题描述】:

我正在做一个项目,每个用户都有自己的坐标 lat / lng,从谷歌地理编码器服务中提取...

现在我想实现一个搜索功能 -> 你可以输入半径(km),如果有人在半径内,就会弹出个人资料...

当搜索区域实际上是一个正方形时,让它工作并不难,但是我如何让它工作,用一个圆圈作为搜索区域?

这是构建 mysql 查询的“搜索”部分的代码:

// This is the location of the user who submitted the search function.
mysql_select_db("$db_profile");
$sql = "SELECT location_lat,
            location_lng
    FROM location
    WHERE id = '$global_id'";
$sql = mysql_query($sql);   
$row = mysql_fetch_array($sql);

// The coordinates of the user.
$lat = $row['location_lat'];
$lng = $row['location_lng'];

// the entered radius in kilometers - here it is converted to degree (1° = 111km).
$x_degree = $global_search_radius_value / 111;

// define the search grid (it is a squere! but I want a circle!).
$lat_neg = $lat - $x_degree;
$lat_pos = $lat + $x_degree;

$lng_neg = $lng - $x_degree;
$lng_pos = $lng + $x_degree;

// build the search part of the mysql query
$heredoc_search_radius = <<<EOD
AND ($db_profile.location.location_lat > '$lat_neg' 
    AND $db_profile.location.location_lat < '$lat_pos'
    AND $db_profile.location.location_lng > '$lng_neg'
    AND $db_profile.location.location_lng < '$lng_pos')
EOD;

如何使用圆形而不是方形!?

【问题讨论】:

    标签: php mysql google-maps google-maps-api-3


    【解决方案1】:

    要保持这种性能,最好的办法是将数据放入 Apache Solr http://lucene.apache.org/solr/features.html 等系统中,这样您就可以执行开箱即用的地理空间搜索。

    无论您拥有多少数据点,Solr 都将确保您的查询保持惊人的速度。

    【讨论】:

      【解决方案2】:

      http://en.wikipedia.org/wiki/Pythagorean_theorem

      在 mysql 中它看起来像这样,如果 ABS 是必要的,则不是 suer,理论上它不应该。

      WHERE SQRT(POW(ABS($db_profile.location.location_lat - $lat), 2) + POW(ABS($db_profile.location.location_lng - $lng), 2)) &lt; $radius

      如果您需要更快的搜索,请将计算出的距离保存在其他表格中。但是,这将使用 n^2 行来存储。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-23
        相关资源
        最近更新 更多