2012-01-16 32 views
0

我正在從事一個項目,每個用戶都有自己的座標緯度/經度,從谷歌地理編碼器服務提取...如何定義lat/lng位置周圍的放射狀搜索區域?

現在我想實現搜索功能 - >你可以輸入半徑(公里)如果有人在半徑範圍內,該配置文件將彈出...

當搜索區域實際上是一個正方形時,它並不難以實現,但是如何獲得此功能以及如何使用作爲搜索區域圈?

這裏是代碼,來構建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; 

我該如何使用,而不是squere了一圈!?

回答

0

爲了保持這種性能,最好的辦法是將數據放入Apache Solr http://lucene.apache.org/solr/features.html這樣的系統中,這樣可以讓您在開箱即可執行地理空間搜索。

Solr將確保您的查詢保持盲目快速,無論您擁有多少數據點。

0

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

在MySQL中它是這樣的,沒有如果發售者ABS是必要的,在理論上它不應該是。

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

如果你需要更快的搜索,請其他表計算出的距離。但是,這將使用n^2行進行存儲。

相關問題